java 多媒体发送邮件

 

import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * 使用简单方式发送一封邮件(普通文本文件)
 * @author Administrator
 *
 */

public class Demo1 {
//    qq
//       pop.qq.com 995     smtp.qq.com 465或587
//    163:
//       imap: imap.163.com 993 143
//       pop3: pop.163.com 995 110
//       smtp: smtp.163.com 465/994 25
//    souhu:
//       pop3.sohu.com    smtp.sohu.com
    
    public static void main(String[] args) throws MessagingException {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "false");
        props.setProperty("mail.transport.protocol", "smtp");
        
        Session session = Session.getDefaultInstance(props);
        //session.setDebug(true);
        
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("from@from.com"));
        msg.setText("Hello world!");
        msg.setSubject("test");
        
        String content = "<div>你不在学校吗?</div><br/><hr/><div>记得28号来学校</div>";
        
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(content, "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        
        //msg.set
        msg.setContent(mainPart);

      String toUser= "**@baidu.com";
InternetAddress mailList[] = InternetAddress.parse(toUser);
String ccUser="**@baidu.com";
InternetAddress ccmailList[] =InternetAddress.parse(ccUser);

msg.setRecipients(Message.RecipientType.TO, mailList);
msg.setRecipients(Message.RecipientType.CC, ccmailList);

Transport trans = session.getTransport();
trans.connect("appmail.baidu.com", 25, "s-baidu", null);
trans.sendMessage(msg, msg.getAllRecipients());
trans.close(); } }

 

posted on 2018-12-20 18:08  corecible  阅读(248)  评论(0编辑  收藏  举报

导航