java 实现邮件发送
2017-07-20 17:06
369 查看
1.下载一个mail.jar架包
http://download.csdn.net/detail/woainimax/9748744
2.AnnexMailService.java
http://download.csdn.net/detail/woainimax/9748744
2.AnnexMailService.java
import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Message.RecipientType; 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 sun.misc.BASE64Encoder; public class AnnexMailService { private String host = ""; private String from = ""; private String to = ""; private String affix = ""; private String affixName = ""; private String user = ""; private String pwd = ""; private String subject = ""; public void setAddress(String from, String to, String subject) { this.from = from; this.to = to; this.subject = subject; } public void setAffix(String affix, String affixName) { this.affix = affix; this.affixName = affixName; } public void send(String host, String user, String pwd) { this.host = host; this.user = user; this.pwd = pwd; Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", Integer.valueOf(465)); props.put("mail.smtp.ssl.enable", Boolean.valueOf(true)); Session session = Session.getDefaultInstance(props); session.setDebug(true); MimeMessage message = new MimeMessage(session); try { message.setFrom(new InternetAddress(this.from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress( this.to)); message.setSubject(this.subject); Multipart multipart = new MimeMultipart(); BodyPart contentPart = new MimeBodyPart(); contentPart.setText("第二种方法···"); multipart.addBodyPart(contentPart); BodyPart messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(this.affix); messageBodyPart.setDataHandler(new DataHandler(source)); BASE64Encoder enc = new BASE64Encoder(); messageBodyPart.setFileName("=?GBK?B?" + enc.encode(this.affixName.getBytes()) + "?="); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); message.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(host, user, pwd); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { AnnexMailService cn = new AnnexMailService(); // 设置发件人地址、收件人地址和邮件标题 cn.setAddress("xxx@163.com", "xxxx@qq.com", "text测试"); // 设置要发送附件的位置和标题 cn.setAffix("D:\\adcfg.json", "abcfg.json"); // 设置smtp服务器以及邮箱的帐号和密码 cn.send("smtp.163.com", "xxx@163.com", "****"); } }
相关文章推荐
- 用java实现的邮件系统六(javamail发送复杂邮件-带附件和html)
- 用Java类实现JavaMail发送邮件的例子
- java实现邮件发送
- java邮件发送 实现群发和带附件发送
- java实现发送邮件的程序
- java发送邮件带附件、格式、可以实现转发、秘密转发等操作
- [转载]javaMail Java实现邮件发送
- java实现邮件的发送包括网页文件的发送
- Java 实现简单的邮件发送
- JAVA邮件发送的简单实现
- java代码实现邮件发送
- 用JSP+SERVLET实现的JAVAMAIL发送邮件
- java实现邮件的发送包括网页文件的发送
- java 实现发送邮件
- 用JavaMial 实现邮件发送
- java实现邮件的发送包括网页文件的发送
- 发送邮件 java实现
- java中如何实现邮件的发送包括网页文件的发送
- 用java实现的邮件系统四 (javamail发送邮件)
- commons-mail+javaMail 实现java的邮件发送功能