pojo
pojo.Email.java
package pojo; import java.util.ArrayList; import java.util.List; public class Email { private String sender; private String senderName; private List<String> receivers = new ArrayList<String>(); private List<String> bccList = new ArrayList<String>(); private List<String> ccList= new ArrayList<String>(); private String subject; private String body; private List<Attachment> attachments = new ArrayList<Attachment>(); public List<Attachment> getAttachments() { return attachments; } public void setAttachments(List<Attachment> attachments) { this.attachments = attachments; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getSenderName() { return senderName; } public void setSenderName(String senderName) { this.senderName = senderName; } public List<String> getReceivers() { return receivers; } public void setReceivers(List<String> receivers) { this.receivers = receivers; } public List<String> getBccList() { return bccList; } public void setBccList(List<String> bccList) { this.bccList = bccList; } public List<String> getCcList() { return ccList; } public void setCcList(List<String> ccList) { this.ccList = ccList; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } }
pojo.Attachment.java
package pojo; public class Attachment { private String attachmentName; private String attachmentLocation; public Attachment() { } public Attachment(String attachmentName, String attachmentLocation) { this.attachmentName = attachmentName; this.attachmentLocation = attachmentLocation; } public String getAttachmentName() { return attachmentName; } public void setAttachmentName(String attachmentName) { this.attachmentName = attachmentName; } public String getAttachmentLocation() { return attachmentLocation; } public void setAttachmentLocation(String attachmentLocation) { this.attachmentLocation = attachmentLocation; } }
helper
helper.DefaultAuthenticator.java
package helper; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class DefaultAuthenticator extends Authenticator { private String userName = null; private String password = null; public DefaultAuthenticator() { } public DefaultAuthenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }
helper.MailOperator.java
package helper; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; 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; import pojo.Attachment; import pojo.Email; import sun.misc.BASE64Encoder; @SuppressWarnings("restriction") public class MailOperator { private static MailOperator mailOperator = new MailOperator(); private MailOperator() { } public static MailOperator getInstance() { return mailOperator; } private Properties getMailProperties() { Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.qq.com"); props.setProperty("mail.smtp.auth", "true"); return props; } private Message getMailMessage(){ Properties props = getMailProperties(); // get the authenticator based on the user name and password DefaultAuthenticator authenticator = new DefaultAuthenticator("username", "password"); // construct the session Session sendMailSession = Session.getDefaultInstance(props,authenticator); sendMailSession.setDebug(true); // create the message Message mailMessage = new MimeMessage(sendMailSession); return mailMessage; } private Multipart getMailContent(String body,List<Attachment> attachments) throws MessagingException, UnsupportedEncodingException{ // add the text content Multipart multipart = new MimeMultipart(); BodyPart textBodyPart = new MimeBodyPart(); // textBodyPart.setText(body); // set the email type as html textBodyPart.setContent(body, "text/html;charset=utf-8"); multipart.addBodyPart(textBodyPart); // add the attachment for(Attachment attachment : attachments){ String attachmentName = attachment.getAttachmentName(); String attachmentLocation = attachment.getAttachmentLocation(); BodyPart attachmentBodyPart= new MimeBodyPart(); // resolve the Chinese characters BASE64Encoder enc = new BASE64Encoder(); attachmentBodyPart.setFileName("=?GBK?B?"+ enc.encode(attachmentName.getBytes()) + "?="); DataSource source = new FileDataSource(attachmentLocation); attachmentBodyPart.setDataHandler(new DataHandler(source)); multipart.addBodyPart(attachmentBodyPart); } return multipart; } public void sendTextMail(Email email) throws Exception{ String sender = email.getSender(); String senderName = email.getSenderName(); String subject = email.getSubject(); String body = email.getBody(); List<String> receivers = email.getReceivers(); List<String> ccList = email.getCcList(); List<String> bccList = email.getBccList(); List<Attachment> attachments = email.getAttachments(); // get the mail message Message mailMessage = getMailMessage(); // set the sender information Address from = new InternetAddress(sender,senderName); mailMessage.setFrom(from); // set the receivers informations for(String receiver : receivers){ Address to = new InternetAddress(receiver); mailMessage.setRecipient(Message.RecipientType.TO, to); } // set the carbon copy information for(String cc : ccList){ Address to = new InternetAddress(cc); mailMessage.setRecipient(Message.RecipientType.CC, to); } // set the blind carbon copy information for(String bcc : bccList){ Address to = new InternetAddress(bcc); mailMessage.setRecipient(Message.RecipientType.BCC, to); } // set the subject information mailMessage.setSubject(subject); // set the send time mailMessage.setSentDate(new Date()); // set the email body information Multipart multipart = getMailContent(body, attachments); mailMessage.setContent(multipart); // send the information Transport.send(mailMessage); } }
测试类 test.Main.java
package test; import helper.MailOperator; import pojo.Attachment; import pojo.Email; public class Main { public static void main(String[] args) throws Exception{ Email email = new Email(); email.setSender("sender email address"); email.setSubject("email subject"); email.setBody("<br/><font color ='red'> email body</font>"); email.getReceivers().add("receiver address"); email.getAttachments().add(new Attachment("attachmentName","attachment location in local server")); MailOperator.getInstance().sendTextMail(email); } }
相关推荐
在你提供的压缩包文件中,包含了三个关键元素:activation.jar、mail.jar以及SendMail.java,这些都是Java Mail发邮件所需的基本组件。 `activation.jar` 是JavaBeans Activation Framework (JAF) 的一部分,它是...
Java Mail 发送邮件及邮件附件 Java Mail 是一个广泛使用的 Java 库,用于发送电子邮件。以下是 Java Mail 发送邮件及邮件附件的知识点。 Java Mail 简介 Java Mail 是一个 Java 库,提供了发送电子邮件的功能。...
### Java Mail 发送邮件知识点详解 #### 一、概述 Java Mail API 是一个用于发送与接收电子邮件的标准 Java 库,支持多种协议如 SMTP (Simple Mail Transfer Protocol) 和 POP3 (Post Office Protocol version 3) ...
Java Mail 是一个用于处理电子邮件的开源API,它允许开发者通过编程方式发送、接收和管理邮件。在Java应用程序中,我们通常使用JavaMail API来实现邮件的自动化操作,比如发送带有附件的邮件、群发邮件等。下面将...
Java Mail 是一个用于处理电子邮件的开源API,它允许开发者通过编程方式发送、接收和操作邮件。这个API提供了广泛的协议支持,包括SMTP(简单邮件传输协议)用于发送邮件,POP3和IMAP4用于接收邮件。在Java Mail中,...
利用Java mail发送邮件,用到的一些类,一些方法,写的很简陋
要使用Java Mail发送邮件,首先需要添加相应的依赖库到项目中。对于Android项目,通常会通过Gradle来引入。 1. 添加依赖: 在项目的build.gradle文件中,添加以下依赖: ```groovy dependencies { ...
下面我们将详细探讨Java Mail发送邮件脚本的核心知识点,以及在实际操作中可能需要的Jar包。 1. **JavaMail API**:JavaMail API是Java中处理电子邮件的标准库,由JavaMail项目维护。它提供了一系列接口和类,包括`...
JavaMail 是一个开源的 Java 库,用于处理电子邮件的发送、接收和其他相关任务。它提供了对各种邮件协议(如 SMTP、POP3 和 IMAP)的支持,并且可以与 MIME 格式的邮件内容配合使用,允许发送包含附件、HTML 内容等...
这个“java mail 发邮件 demo程序”是为了演示如何使用JavaMail API发送电子邮件而创建的一个简单示例。在这个DEMO程序中,我们将深入探讨JavaMail API的核心概念以及如何构建一个实际的邮件发送功能。 首先,...
这个标题“Java mail发送邮件(源代码带注释)”表明我们将讨论如何使用JavaMail API来实现电子邮件的发送功能。描述提到,通过简单的赋值和测试,我们可以快速实现邮件发送,这表明提供的源代码应该是一个易于理解且...
Java Mail 是一个用于处理电子邮件的开源API,它允许开发者通过编程方式发送、接收和管理邮件。这个源码可能包含了一套完整的解决方案,可以与多种邮局(如Gmail, Yahoo, Hotmail等)进行交互,并且支持单个收件人和...
Java Mail 是一个强大的开源API,用于处理电子邮件的发送和接收。这个API允许开发者在Java应用程序中集成邮件功能,包括SMTP(简单邮件传输协议)和POP3(邮局协议第三版)等邮件服务协议。本教程将详细介绍如何使用...
以下是一个简单的Java Mail发送邮件的示例代码: ```java import javax.mail.*; import javax.mail.internet.*; public class EmailSender { public static void main(String[] args) throws Exception { // ...
要使用Java Mail发送QQ邮件,首先需要在QQ邮箱设置中开启“POP3/SMTP/IMAP”服务,并获取授权码。授权码是QQ邮箱为第三方应用提供的一种安全验证方式,不同于普通密码。 ### 3. 添加依赖 在Java项目中,我们需要...
Java Mail邮件发送是Java开发中常见的一项功能,用于在应用程序中发送电子邮件。Java Mail API提供了一个标准的接口,使得开发者可以方便地实现SMTP(简单邮件传输协议)和其他邮件协议的功能。下面将详细介绍Java ...
下面是一个简化的Java Mail发送邮件的步骤,展示了一些关键API的使用: 1. 引入Java Mail依赖:首先确保项目中包含了Java Mail的相关库,通常通过Maven或Gradle等构建工具添加依赖。 2. 创建Properties对象:设置...