java邮件发送(以163邮箱为例)

1.首先应该开通163邮箱的smtp和pop3,得到授权码

2.其次建立一个web项目,否则需要倒jar包mail.jar

3.创建一个类

4.注意:邮件内容必须为正式话语,否则系统会认为是垃圾邮件而拒收,报错541DT

public static void main(String[] args) throws MessagingException {
Properties prop=new Properties();
prop.put("mail.host","smtp.163.com" );
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.auth", true);
//使用java发送邮件5步骤
//1.创建sesssion
Session session=Session.getInstance(prop);
//开启session的调试模式,可以查看当前邮件发送状态
session.setDebug(true);


//2.通过session获取Transport对象(发送邮件的核心API)
Transport ts=session.getTransport();
//3.通过邮件用户名密码链接
ts.connect("此处应为用户名", "此处应为授权码");


//4.创建邮件

Message msg=createSimpleMail(session);


//5.发送电子邮件

ts.sendMessage(msg, msg.getAllRecipients());
}

 
public static MimeMessage createSimpleMail(Session session) throws AddressException,MessagingException{
//创建邮件对象
MimeMessage mm=new MimeMessage(session);
//设置发件人
mm.setFrom(new InternetAddress("用户名@163.com"));
//设置收件人
mm.setRecipient(Message.RecipientType.TO, new InternetAddress("用户名@163.com"));
//设置抄送人
mm.setRecipient(Message.RecipientType.CC, new InternetAddress("用户名@163.com"));

mm.setSubject("第一封JAVA邮件!");
mm.setContent("咱们开会把", "text/html;charset=gbk");

return mm;

}

package com.lccert.crm.chemistry.util; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendMail { private static SendMail instance = null; private SendMail() { } public static SendMail getInstance() { if (instance == null) { instance = new SendMail(); } return instance; } public void send() { try { String to[]={"tiwsonchen@163.com","tiwson@163.com"}; Properties p = new Properties(); //Properties p = System.getProperties(); p.put("mail.smtp.auth", "true"); p.put("mail.transport.protocol", "smtp"); p.put("mail.smtp.host", "smtp.163.com"); p.put("mail.smtp.port", "25"); //建立会话 Session session = Session.getInstance(p); Message msg = new MimeMessage(session); //建立信息 msg.setFrom(new InternetAddress("tiwson@163.com")); //发件人 String toList = getMailList(to); InternetAddress[] iaToList = new InternetAddress().parse(toList); msg.setRecipients(Message.RecipientType.TO,iaToList); //收件人 msg.setSentDate(new Date()); // 发送日期 msg.setSubject("javamail测试邮件"); // 主题 msg.setText("注意,这是测试程序发的,请不要回复!"); //内容 // 邮件服务器进行验证 Transport tran = session.getTransport("smtp"); tran.connect("smtp.163.com", "tiwson", "9041160"); // bluebit_cn是用户名,xiaohao是密码 tran.sendMessage(msg, msg.getAllRecipients()); // 发送 System.out.println("邮件发送成功"); } catch (Exception e) { e.printStackTrace(); } } private String getMailList(String[] mailArray){ StringBuffer toList = new StringBuffer(); int length = mailArray.length; if(mailArray!=null && length <2){ toList.append(mailArray[0]); }else{ for(int i=0;i<length;i++){ toList.append(mailArray[i]); if(i!=(length-1)){ toList.append(","); } } } return toList.toString(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值