应用java mail和阿里云发送邮件和上传附件
来源:互联网 发布:linux 开启snmp 编辑:程序博客网 时间:2024/05/19 13:05
package com.mail;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class SendMail {
public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
Properties prop=new Properties();
prop.put("mail.host","smtp.aliyun.com" );
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.auth", "true");
Session session=Session.getInstance(prop);
session.setDebug(true);
Transport ts=session.getTransport();
ts.connect("qinxin0823@aliyun.com", "123456"); //登录的账户名 和 密码
Message msg=createSimpleMail(session);
ts.sendMessage(msg, msg.getAllRecipients());
}
public static MimeMessage createSimpleMail(Session session) throws AddressException,MessagingException, UnsupportedEncodingException{
MimeMessage mm=new MimeMessage(session);
mm.setFrom(new InternetAddress("qinxin0823@aliyun.com")); //设置发件人
mm.setRecipient(Message.RecipientType.TO, new InternetAddress("274788664@qq.com")); //设置收件人
//mm.setRecipient(Message.RecipientType.CC, new InternetAddress("XXXX@qq.com")); //设置抄送人
mm.setSubject("清算清单!");
Multipart multipart = new MimeMultipart();
String mailBody = "请查收";
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(mailBody, "text/html;charset=utf-8");
multipart.addBodyPart(bodyPart);
BodyPart bodyPart1 = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource("c:/投资(或清算)总表.xlsx");
bodyPart1.setDataHandler(new DataHandler(fileDataSource));
bodyPart1.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
multipart.addBodyPart(bodyPart1);
mm.setContent(multipart);
mm.saveChanges();
return mm;
}
}
0 0
- 应用java mail和阿里云发送邮件和上传附件
- linux发送邮件和附件---mail,mailx
- java Mail发送附件邮件
- Java Mail 发送附件的邮件
- 带附件的java.mail邮件发送
- Java Mail 发送带附件邮件
- Java Mail发送邮件(带多个附件)
- java mail发送邮件(单发和群发)
- Java Mail简化邮件发送+附件发送(源码奉上)
- C++发送邮件和附件
- C++发送邮件和附件
- C++发送邮件和附件
- smtp发送邮件和附件
- java邮件发送 实现群发和带附件发送
- 阿里云专用网络mail邮件发送
- 使用java mail发送带附件的邮件
- java mail发送邮件可群发并带附件
- Java Mail实现带附件的邮件发送
- 总结的前端入门方法
- eclipse 当中,小白们所不知道的 CRTL+1 快捷键
- JSP文件和Servlet在文件目录以及服务器中配置的不同
- 自定义ViewGroup-自定义LayoutParams支持显示方位
- 排序
- 应用java mail和阿里云发送邮件和上传附件
- 使用HTML5制作云层效果
- TNS:listener could not find available handler with matching protocol stack 解决办法
- 斐波那契额数列
- 机器学习之-分类性能指标-准确率-召回率-ROC曲线
- ZARCHIVE_WRITE(限制用于Z表)
- FFMPEG 图像拉伸缩放及数据格式转换
- 软件测试面试题
- C#学习心得