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); } }
相关推荐
java mail发送邮件应用的jar,还有一个activation也要一起引用
利用Java mail发送邮件,用到的一些类,一些方法,写的很简陋
java Mail 发送邮件及邮件附件 java Mail 发送邮件及邮件附件
使用 java mail 发送邮件
java mail 发送邮件源代码以及所需要的两个架包。 附件代码以腾讯企业邮箱发送邮件为例子,其它邮箱只要支持SMTP服务即可。只要懂点JAVA就能看懂。
java mail详细发送邮件代码
MAIL. JAVA POP3 SMTP imap 发送邮件
用java mail发邮件源码,支持多种邮局,支持单发和群发。
包含文件: activation.jar mail.jar SendMail.java
Java实现发送邮件脚本。以及需要调用的全部Jar包。
为了以后使用java发送邮件的需要。结合网上案例做了一个demo程序。javamail发送邮件确实是一个比较麻烦的问题。该demo 程序可以直接用于邮件的发送.
java 发送邮件 spring发送邮件Mail
只需简单赋几个值便可实现发送邮件,里边有源代码、全有注释,可直接在main方法内测试发送,非常易学实用
java mail发送qq邮件的一个例子,谢谢大家的支持
也许很多 同道遇到和我一样的问题,建了个web项目,写一堆代码,结果一运行老是报错!!!上网查了查,知道了原因,修改了还是不行。...如果还没有成功,你可以给我发邮件973203075@qq.com,我可以把项目也给你!!
JavaMail用到的jar包,一共3个。下载之后要放到libs目录下,然后右击工程,选择Builder Path--》Configure Build Path,Libraries,Add JARs,选择工程目录下的libs下的jar包。
java发送附件mail, 包阔多附件,文本mail,以及网页mail.
基于javax.mail的邮件发送测试工具