smtp协议邮件转发

举报
Amrf 发表于 2018/12/11 17:10:33 2018/12/11
【摘要】 smtp协议介绍Simple Mail Transfer Protocol (SMTP) is an Internet standard for electronic mail (email) transmission. First defined by RFC 821 in 1982, it was updated in 2008 with Extended SMTP additions ...

smtp协议介绍

Simple Mail Transfer Protocol (SMTP) is an Internet standard for electronic mail (email) transmission. First defined by RFC 821 in 1982, it was updated in 2008 with Extended SMTP additions by RFC 5321, which is the protocol in widespread use today.

Session.getInstance vs Session.getDefaultInstance

https://stackoverflow.com/questions/4184204/what-is-the-difference-between-getdefaultinstance-and-getinstance-in-session

发送文本邮件

public boolean sendTextMail(String recipients, String subject, String message) throws AddressException, MessagingException, AuthenticationFailedException
{   
    try
    {    
// 获取邮件会话属性.
Properties props = getProperties();    
HashMap<String,String> ac = getMailAC(); 
// 根据邮件会话属性和密码验证器构造一个发送邮件的session.  
Session mailSession = Session.getInstance(props, new Authenticator()//Default
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(ac.get("userName"), ac.get("userPwd"));
            }
        });     
// 根据session创建一个邮件消息.
Message mailMessage = new MimeMessage(mailSession);  
// 设置发件人地址.
InternetAddress o_fromAddress = new InternetAddress(ac.get("fromAddress"));  
mailMessage.setFrom(o_fromAddress); 
// 处理收件人地址. 
String mail_recipients = recipients.replace(",", ";");
String[] mail_recipients_list = mail_recipients.split(";");  
InternetAddress[] toAddressList = new InternetAddress[mail_recipients_list.length];  
// 添加收件人地址. 
    for (int i = 0; i < mail_recipients_list.length; i++)   
    {
    String curr_recipient = mail_recipients_list[i];
    curr_recipient.replace(" ", "");
    toAddressList[i] = new InternetAddress(curr_recipient);   
    }
    mailMessage.setRecipients(Message.RecipientType.TO, toAddressList);
    // 添加抄送人地址(默认抄送给发件人).
InternetAddress[] ccAddressList = new InternetAddress[1];  
ccAddressList[0] = o_fromAddress;
    mailMessage.setRecipients(Message.RecipientType.CC, ccAddressList);
// 设置邮件标题.   
((MimeMessage)mailMessage).setSubject(subject, "GBK");
// 设置邮件正文.
mailMessage.setText(message);
// 设置发送日期.
mailMessage.setSentDate(new Date());
// 发送邮件.
Transport.send(mailMessage);
// 设置函数返回值.
return true;
    }
    catch (AddressException e)
    {
log4j.error(Debug.getFileName() 
+ ":" + Debug.getLineNumber()
+ ":" + e.toString());
    }
    catch (MessagingException e)
    {
log4j.error(Debug.getFileName() 
+ ":" + Debug.getLineNumber()
+ ":" + e.toString());
    }       
// 设置函数返回值.
return false;
} /* End of 发送文本邮件. */

发送html形式的邮件

public boolean sendHtmlMail(String recipients, String mailCC, String subject, String message) throws AddressException, MessagingException, AuthenticationFailedException
{   
    try
    {    
// 获取邮件会话属性.
Properties props = getProperties();           
HashMap<String,String> ac = getMailAC(); 
// 根据邮件会话属性和密码验证器构造一个发送邮件的session.  
Session mailSession = Session.getInstance(props, new Authenticator()//Default
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(ac.get("userName"), ac.get("userPwd"));
            }
        });     
// 根据session创建一个邮件消息.
Message mailMessage = new MimeMessage(mailSession);  
// 设置发件人地址.
InternetAddress o_fromAddress = new InternetAddress(ac.get("fromAddress"));  
mailMessage.setFrom(o_fromAddress); 
// 处理收件人地址. 
String mail_recipients = recipients.replace(",", ";");
String[] mail_recipients_list = mail_recipients.split(";");  
InternetAddress[] toAddressList = new InternetAddress[mail_recipients_list.length];  
// 添加收件人地址. 
    for (int i = 0; i < mail_recipients_list.length; i++)   
    {
    String curr_recipient = mail_recipients_list[i];
    curr_recipient.replace(" ", "");
    toAddressList[i] = new InternetAddress(curr_recipient);   
    }
    mailMessage.setRecipients(Message.RecipientType.TO, toAddressList);
    // 处理收件人地址. 
String mail_ccRecipients = mailCC.replace(",", ";");
String[] mail_ccRecipients_list = mail_ccRecipients.split(";");  
InternetAddress[] ccAddressList = new InternetAddress[mail_ccRecipients_list.length];  
// 添加抄送人地址(默认抄送给发件人).
for (int i = 0; i < mail_ccRecipients_list.length; i++)   
    {
    String curr_recipient = mail_ccRecipients_list[i];
    curr_recipient.replace(" ", "");
    ccAddressList[i] = new InternetAddress(curr_recipient);   
    }
    mailMessage.setRecipients(Message.RecipientType.CC, ccAddressList);
// 设置邮件标题.
((MimeMessage)mailMessage).setSubject(subject, "GBK");
// 创建一个MiniMultipart容器,用于包含MimeBodyPart类型的对象.
MimeMultipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart.
MimeBodyPart htmlBody = new MimeBodyPart();
// 设置HTML内容.  
htmlBody.setContent(message, "text/html; charset=utf-8");
mainPart.addBodyPart(htmlBody);
// 将MiniMultipart对象设置为邮件内容.
mailMessage.setContent(mainPart);   
// 设置发送日期.
mailMessage.setSentDate(new Date());
// 发送邮件.
Transport.send(mailMessage);
// 设置函数返回值.
return true;
    }
    catch (AddressException e)
    {
log4j.error(Debug.getFileName() 
+ ":" + Debug.getLineNumber()
+ ":" + e.toString());
    }
    catch (MessagingException e)
    {
log4j.error(Debug.getFileName() 
+ ":" + Debug.getLineNumber()
+ ":" + e.toString());
    }    
// 设置函数返回值.
return false;
} /* End of 发送HTML邮件. */


【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。