javax mail发送邮件

本文提供了一个使用Java发送带附件的电子邮件的示例代码。通过配置SMTP服务器,并利用MIME多部分消息格式来构建带有HTML内容及文件附件的邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package qianmi.test.QR;


import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.Properties;


import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;


public class sendEmailTest3 {




  public static void main(String[] args) {
        try {
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.163.com");
prop.put("mail.smtp.port", "25");
prop.put("mail.smtp.auth", "true");
// prop.setProperty("mail.debug", "true");
   Authenticator authenticator = new MyAuthetic("from11@163.com", "sss");//(password)授权密码
   //创建会话
   Session session = Session.getInstance(prop,authenticator);
   //填写信封写信
/*   Message msg = new MimeMessage(session);
   msg.setFrom(new InternetAddress("1604615757@qq.com"));*/
   
   
   MimeMessage mailMessage = new MimeMessage(session);
       mailMessage.setFrom(new InternetAddress("from11@163.com"));
       // Message.RecipientType.TO属性表示接收者的类型为TO
       mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("1to@qq.com"));
//        mailMessage.setSubject(subject, "UTF-8");
       mailMessage.setSentDate(new Date());
      // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
       Multipart mainPart = new MimeMultipart();
       // 创建一个包含HTML内容的MimeBodyPart
       BodyPart html = new MimeBodyPart();
       html.setContent("love", "text/html; charset=utf-8");
       mainPart.addBodyPart(html);
       mailMessage.setSubject("love");
//        mailMessage.setText("love");
      


       BodyPart filepart=new MimeBodyPart();
       String url=sendEmailTest3.class.getClassLoader().getResource("").getPath(); //src目录
       File file=new File(url.toString(),"ss.xls");
       DataSource  ds=new FileDataSource(file);
       DataHandler dtd=new DataHandler(ds);
       filepart.setDataHandler(dtd);
       filepart.setFileName("ss.xls");
       mainPart.addBodyPart(filepart);
       
       
       mailMessage.setContent(mainPart);
       
//        Transport.send(mailMessage);
       
       System.out.println("ccccccccccccc");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


  }



}





package qianmi.test.QR;


import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


public class MyAuthetic extends Authenticator {
String userName = null;
     String password = null;
     public MyAuthetic() {
     }
     public MyAuthetic(String username, String password) {
         this.userName = username;
         this.password = password;
     }
     protected PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(userName, password);
     }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值