工具篇:Java中邮件类MimeMessage说明

MimeMessage类位于javax.mail.internet包下,是用于发送邮件的一个类,其中header变量包含了众多与邮件头信息有关的变量

headers头信息
索引序号变量名含义
0Return-Path
1Received
2Resent-Date
3Resent-From
4Resent-Sender
5Resent-To
6Resent-Cc
7Resent-Bcc
8Resent-Message-Id
9Date发送时间
10From发件人
11Sender
12Reply-To
13To收件人
14Cc抄送人
15

Bcc

密送人
16Message-Id
17In-Reply-To
18References
19Subject邮件主题
20Comments
21Keywords
22Errors-To
23MIME-Version
24Content-Type
25Content-Transfer-Encoding
26Content-MD5
27
28Content-Length
29Status

a

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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[]={"[email protected]","[email protected]"}; 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("[email protected]")); //发件人 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(); } }
下面是一个简单的 Java 发送邮件工具类,你可以根据自己的需求进行修改: ``` import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MailUtil { private String host; // 邮件服务器主机名 private String username; // 发件人账号 private String password; // 发件人密码 public MailUtil(String host, String username, String password) { this.host = host; this.username = username; this.password = password; } /** * 发送邮件 * @param to 收件人邮箱地址 * @param subject 邮件主题 * @param content 邮件内容 */ public void sendMail(String to, String subject, String content) throws MessagingException { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", host); props.setProperty("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setContent(content, "text/html;charset=utf-8"); Transport.send(message); } } ``` 使用示例: ``` public static void main(String[] args) { String host = "smtp.163.com"; String username = "[email protected]"; String password = "your_email_password"; MailUtil mailUtil = new MailUtil(host, username, password); String to = "[email protected]"; String subject = "测试邮件"; String content = "这是一封测试邮件,如果你收到了,请回复我。"; try { mailUtil.sendMail(to, subject, content); System.out.println("邮件发送成功!"); } catch (MessagingException e) { System.out.println("邮件发送失败:" + e.getMessage()); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值