第一范文网 - 专业文章范例文档资料分享平台

java发送电子邮件

来源:用户分享 时间:2025/7/13 5:54:09 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

1 邮件的基本信息类 packagecom.pojos;

importjava.util.Properties; /**

* 发送邮件需要使用的基本信息 * author by wangfun http://www.5a520.cn 小说520 * @author dell * */

publicclassMailSenderInfo { // 发送邮件的服务器的IP和端口 private String mailServerHost; private String mailServerPort = \; // 邮件发送者的地址 private String fromAddress; // 邮件接收者的地址 private String toAddress; // 登陆邮件发送服务器的用户名和密码 private String userName; private String password; // 是否需要身份验证 privatebooleanvalidate = false; // 邮件主题 private String subject; // 邮件的文本内容 private String content; // 邮件附件的文件名 private String[] attachFileNames; /** */ /** * 获得邮件会话属性 */ public Properties getProperties() { Properties p = newProperties(); p.put(\, this.mailServerHost); p.put(\, this.mailServerPort); p.put(\, validate ? \ :\); return p; } public String getMailServerHost() { returnmailServerHost; } publicvoidsetMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { returnmailServerPort; }

publicvoidsetMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; }

publicbooleanisValidate() { returnvalidate; }

publicvoidsetValidate(boolean validate) { this.validate = validate; }

public String[] getAttachFileNames() { returnattachFileNames; }

publicvoidsetAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; }

public String getFromAddress() { returnfromAddress; }

publicvoidsetFromAddress(String fromAddress) { this.fromAddress = fromAddress; }

public String getPassword() { returnpassword; }

publicvoidsetPassword(String password) { this.password = password; }

public String getToAddress() { returntoAddress; }

publicvoidsetToAddress(String toAddress) { this.toAddress = toAddress; }

public String getUserName() { returnuserName; }

publicvoidsetUserName(String userName) { this.userName = userName; }

public String getSubject() { returnsubject; }

publicvoidsetSubject(String subject) {

}

this.subject = subject; }

public String getContent() { returncontent; }

publicvoidsetContent(String textContent) { this.content = textContent; }

1 发送器

packagecom.pojos;

importjava.util.Date; importjava.util.Properties; importjavax.mail.Address; importjavax.mail.BodyPart; importjavax.mail.Message;

importjavax.mail.MessagingException; importjavax.mail.Multipart; importjavax.mail.Session; importjavax.mail.Transport;

importjavax.mail.internet.InternetAddress; importjavax.mail.internet.MimeBodyPart; importjavax.mail.internet.MimeMessage; importjavax.mail.internet.MimeMultipart;

/** *//**

* 简单邮件(不带附件的邮件)发送器 http://www.bt285.cn BT下载 */

public class SimpleMailSender { /** *//**

* 以文本格式发送邮件

* @parammailInfo待发送的邮件的信息 */

publicbooleansendTextMail(MailSenderInfomailInfo) { // 判断是否需要身份认证

MyAuthenticator authenticator = null;

Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) {

// 如果需要身份认证,则创建一个密码验证器

authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); }

// 根据邮件会话属性和密码验证器构造一个发送邮件的session

Session sendMailSession = Session.getDefaultInstance(pro,authenticator); try {

// 根据session创建一个邮件消息

Message mailMessage = new MimeMessage(sendMailSession); // 创建邮件发送者地址

Address from = new InternetAddress(mailInfo.getFromAddress()); // 设置邮件消息的发送者 mailMessage.setFrom(from);

// 创建邮件的接收者地址,并设置到邮件消息中

Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO,to); // 设置邮件消息的主题

mailMessage.setSubject(mailInfo.getSubject()); // 设置邮件消息发送的时间

mailMessage.setSentDate(new Date()); // 设置邮件消息的主要内容

String mailContent = mailInfo.getContent(); mailMessage.setText(mailContent); // 发送邮件

Transport.send(mailMessage); return true;

} catch (MessagingException ex) { ex.printStackTrace(); } return false; }

/** *//**

* 以HTML格式发送邮件

* @parammailInfo待发送的邮件信息 */

public static booleansendHtmlMail(MailSenderInfomailInfo){ // 判断是否需要身份认证

MyAuthenticator authenticator = null;

Properties pro = mailInfo.getProperties(); //如果需要身份认证,则创建一个密码验证器 if (mailInfo.isValidate()) { //创建一个密码验证器

authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); }

// 根据邮件会话属性和密码验证器构造一个发送邮件的session

Session sendMailSession = Session.getDefaultInstance(pro,authenticator); try {

// 根据session创建一个邮件消息

Message mailMessage = new MimeMessage(sendMailSession); // 创建邮件发送者地址

Address from = new InternetAddress(mailInfo.getFromAddress()); // 设置邮件消息的发送者 mailMessage.setFrom(from);

// 创建邮件的接收者地址,并设置到邮件消息中

Address to = new InternetAddress(mailInfo.getToAddress()); // Message.RecipientType.TO属性表示接收者的类型为TO mailMessage.setRecipient(Message.RecipientType.TO,to); // 设置邮件消息的主题

mailMessage.setSubject(mailInfo.getSubject()); // 设置邮件消息发送的时间

mailMessage.setSentDate(new Date());

// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象 Multipart mainPart = new MimeMultipart(); // 创建一个包含HTML内容的MimeBodyPart BodyPart html = new MimeBodyPart(); // 设置HTML内容

html.setContent(mailInfo.getContent(), \ mainPart.addBodyPart(html);

// 将MiniMultipart对象设置为邮件内容 mailMessage.setContent(mainPart); // 发送邮件

Transport.send(mailMessage); return true;

} catch (MessagingException ex) { ex.printStackTrace(); } return false; } }

搜索更多关于: java发送电子邮件 的文档
java发送电子邮件.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c4xl8p0n7n376vac3m04a_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top