公司郵箱發郵件的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;

    }

}

相關文章
相關標籤/搜索