首页 > 代码库 > JavaMail发送邮件
JavaMail发送邮件
引入了一个jar:javax.mail.jar
直接上代码:
InitMail:
package com.hi.test;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Address;import javax.mail.BodyPart;import javax.mail.Message;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;public class InitMail { private String smtp;// SMTP地址 private String from;// 发件人 private String to;// 收件人 private String copyto;// 抄送 private String subject;// 主题 private String content;// 内容 private String username;// 用户名 private String password;// 密码 private String filename;// 附件地址,如: S:\download\xxx.txt // =====================// private MimeMessage mimeMsg; // MIME邮件对象 private Session session; // 邮件会话对象 private Properties props; // 系统属性 private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 // ======================// @SuppressWarnings("unused") private InitMail() { } /* * 抄送和附件可以为空,其他不能为空 */ public InitMail(String smtp, String from, String to, String copyto, String subject, String content, String username, String password, String filename) { this.smtp = smtp; this.from = from; this.to = to; this.copyto = copyto; this.subject = subject; this.content = content; this.username = username; this.password = password; this.filename = filename; } public boolean sendMail() { boolean result = false; try { props = System.getProperties(); // 获得系统属性对象 session = Session.getDefaultInstance(props); // 获得邮件会话对象 mimeMsg = new MimeMessage(session); // 创建MIME邮件对象 mp = new MimeMultipart(); mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人 mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));//设置收件人 mimeMsg.setSubject(""+subject);//设置主题 //如果抄送人不为空或null,则设置抄送人 if(!(null==copyto||"".equals(copyto))){ mimeMsg.setRecipients(Message.RecipientType.CC,(Address[]) InternetAddress.parse(copyto)); } BodyPart bp = new MimeBodyPart(); bp.setContent("" + content, "text/html;charset=GBK"); mp.addBodyPart(bp); //如果附件不为空或null,则设置附件 if(!(null==filename||"".equals(filename))){ BodyPart bp2 = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filename); bp2.setDataHandler(new DataHandler(fileds)); bp2.setFileName(fileds.getName()); mp.addBodyPart(bp2); } mimeMsg.setContent(mp); mimeMsg.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(smtp, username,password); transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO)); //如果抄送人不为空或null,则发送给抄送人 if(!(null==copyto||"".equals(copyto))){ transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC)); } transport.close(); result=true; System.out.println("发送成功"); } catch (Exception e) { System.out.println("出错,邮件发送失败"); e.printStackTrace(); } return result; } }
SendMail:
package com.hi.test;public class SendMail { public static void main(String[] args) { // TODO Auto-generated method stub String smtp = "smtp.qq.com"; String from = "xxx@qq.com"; String to = "xxx@hotmail.com"; String subject = "test"; String content = "this is java mail test "; String username="xxx"; String password="xxx"; String filename="S:\\download\\javax.mail.jar"; InitMail mail=new InitMail(smtp, from, to, null, subject,content, username, password, filename); mail.sendMail(); }}
main中账号密码换成自己的就可以了,如果没有抄送和附件,传入null就可以了。具体的可以看代码,没什么太多需要说的。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。