公司邮箱发邮件的util类

以前有用126,163等邮箱发送邮件成功,可是用公司的阿里云邮箱因为设置问题则不行。如今把解决了的util类贴出来供参考。html

package sunallies.util;java

 

import javax.mail.*;服务器

import javax.mail.internet.InternetAddress;session

import javax.mail.internet.MimeMessage;测试

import java.util.Properties;阿里云

 

/**debug

 * Created by lovywinsy on 6/21/16.htm

 */ip

public class MailUtil {ci

 

    public static void main(String[] args) throws Exception {

        Properties prop = new Properties();

        prop.setProperty("mail.smtp.host", "smtp.sunallies.com");

        prop.setProperty("mail.transport.protocol", "smtp");

        prop.setProperty("mail.smtp.auth", "true");

        //建立session并开启debug

        Session session = Session.getInstance(prop);

        session.setDebug(true);

        //经过session获得transport

        Transport transport = session.getTransport();

        //链接smtp服务器

        transport.connect("smtp.sunallies.com", "zhuxiao@sunallies.com", "");

        //建立邮件

        Message message = createSimpleMail(session);

        //发送邮件

        transport.sendMessage(message, message.getAllRecipients());

        transport.close();

    }

 

    public static MimeMessage createSimpleMail(Session session) throws Exception {

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress("zhuxiao@sunallies.com"));

        message.setRecipient(Message.RecipientType.TO, new InternetAddress("zhuxiao@sunallies.com"));

        message.setSubject("自动化测试报告");

        message.setContent("都经过了", "text/html;charset=UTF-8");

        return message;

    }

}

相关文章
相关标签/搜索