码迷,mamicode.com
首页 > 编程语言 > 详细

Java 邮件发送

时间:2019-03-22 00:28:14      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:bsp   set   image   测试   message   public   名称   end   net   

<groupId>com.sun.mail </groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
</dependency>

 

服务器的一些配置参数:

详情可以点开下面链接

https://blog.csdn.net/ghjzzhg/article/details/77838880

 

 

直接上代码

public static void sendEmail(EmailMsg msg){
//邮箱
String myAccount = "a769104932@qq.com";   //放上自己的邮箱
//授权码
String myPass = "XXXXXX";   //这里授权码 需要根据服务器开启授权码
//邮箱服务器
String SMTPHost = "smtp.qq.com"; //根据自己的服务器开启smtphost
//设置属性信息
Properties prop = new Properties();
//设置协议
prop.setProperty("mail.transport.protocol", "smtp");
//邮件服务器
prop.setProperty("mail.smtp.host", SMTPHost);
//认证
prop.setProperty("mail.smtp.auth", "true");
//1、创建会话
Session session = Session.getDefaultInstance(prop);
//设置是否需要调试
session.setDebug(false);
//2、创建发送信息
MimeMessage message = createMsg(session,myAccount,msg);
//4发送信息操作
try {
Transport tran = session.getTransport();
//连接
tran.connect(myAccount, myPass);
//发送消息
tran.sendMessage(message, message.getAllRecipients());
//关闭
tran.close();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

 

 

//生成邮件消息
private static MimeMessage createMsg(Session session, String myAccount, EmailMsg msg) {
//创建消息对象
MimeMessage message = new MimeMessage(session);
//设置
try {
//3.1发送方
message.setFrom(new InternetAddress(myAccount, msg.getCompany(), "utf-8"));
//3.2设置接收方
/*
* MimeMessage.RecipientType.TO
* MimeMessage.RecipientType.CC
* MimeMessage.RecipientType.BCC
* */
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(msg.getEmail(), "万元户", "utf-8"));
//3.3 设置主题
message.setSubject(msg.getTitle(),"utf-8");
//设置正文信息
message.setContent(msg.getContent(),"text/html;charset=utf-8");
//设置日期
message.setSentDate(new Date());
//保存
message.saveChanges();
} catch (Exception e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return message;
}

 

//定义一个邮件类

public class EmailMsg {
// 公司名称
private String company;
// 标题
private String Title;
// 内容
private String Content;
// 邮件
private String Email;

public String getCompany() {
return company;
}

public void setCompany(String company) {
this.company = company;
}

public String getTitle() {
return Title;
}

public void setTitle(String title) {
Title = title;
}

public String getContent() {
return Content;
}

public void setContent(String content) {
Content = content;
}

public String getEmail() {
return Email;
}

public void setEmail(String email) {
Email = email;
}

 

 

//测试

EmailMsg el = new EmailMsg();
el.setTitle("测试主题");
el.setCompany("千峰");
el.setContent("我就是陆哥哥");
el.setEmail("a769104932@qq.com");
EmailUtil.sendEmail(el);
}

技术图片

 

 成功!

 

Java 邮件发送

标签:bsp   set   image   测试   message   public   名称   end   net   

原文地址:https://www.cnblogs.com/jiaowoxiaofeng/p/10575625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!