[置顶] JAVA实现发送邮件(图片和附件)
2016-09-06 17:10
639 查看
java实现发送邮件功能
已网易为例
首先注册网易账号–>网易注册在邮箱设置里面把POP3开启,只有开启了才能使用程序来发送邮件了
POP3全名为“Post Office Protocol - Version 3”,即“邮局协议版本3”。是TCP/IP协议族中的一员,由RFC1939 定义。本协议主要用于支持使用客户端远程管理在服务器上的电子邮件。提供了SSL加密的POP3协议被称为POP3S。–百度百科
准备工作做好后开始写代码啦!
代码实现功能1.普通邮件
**下面写的Password就是刚才设置的授权密码
代码
import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Message; 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; /** * 发送普通邮件 * * @author Administrator * */ public class Email { public static void main(String[] args) throws Exception { Properties props = new Properties(); // 开启debug调试 props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "true"); // 设置邮件服务器主机名 props.setProperty("mail.host", "smtp.163.com"); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); Session session = Session.getInstance(props); Message msg = new MimeMessage(session); msg.setSubject("小张调试邮件"); StringBuilder builder = new StringBuilder(); builder.append("url = " + "http://www.baidu.com"); builder.append("\n欢迎您!注册易买网仅此一封呵呵"); msg.saveChanges(); msg.setText(builder.toString()); msg.setFrom(new InternetAddress("13155903710@163.com")); Transport transport = session.getTransport(); transport.connect("smtp.163.com", "13155903710@163.com", "z1234567"); transport.sendMessage(msg, new Address[] { new InternetAddress( "978108040@qq.com") }); transport.close(); } public static MimeBodyPart createContent(String body, String fileName) throws Exception { // 用于保存最终正文部分 MimeBodyPart contentBody = new MimeBodyPart(); // 用于组合文本和图片,"related"型的MimeMultipart对象 MimeMultipart contentMulti = new MimeMultipart("related"); // 正文的文本部分 MimeBodyPart textBody = new MimeBodyPart(); textBody.setContent(body, "text/html;charset=gbk"); contentMulti.addBodyPart(textBody); // 正文的图片部分 MimeBodyPart jpgBody = new MimeBodyPart(); FileDataSource fds = new FileDataSource(fileName); jpgBody.setDataHandler(new DataHandler(fds)); jpgBody.setContentID("logo_jpg"); contentMulti.addBodyPart(jpgBody); // 将上面"related"型的 MimeMultipart 对象作为邮件的正文 contentBody.setContent(contentMulti); return contentBody; } }
效果图
2.带图片的邮件
代码
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.MessagingException; import javax.mail.PasswordAuthentication; 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; /** * 发送内嵌图片的邮件 * * @author Administrator * */ public class emailImage { public static void main(String[] args) { // Recipient's email ID needs to be mentioned. String to = "978108040@qq.com"; // Sender's email ID needs to be mentioned String from = "13155903710@163.com"; final String username = "13155903710@163.com";// change accordingly final String password = "z1234567";//授权密码非邮箱登录密码 // Assuming you are sending email through relay.jangosmtp.net String host = "relay.jangosmtp.net"; Properties props = new Properties(); // 开启debug调试 props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "true"); // 设置邮件服务器主机名 props.setProperty("mail.host", "smtp.163.com"); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Testing Subject"); // This mail has 2 part, the BODY and the embedded image MimeMultipart multipart = new MimeMultipart("related"); // first part (the html) BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>Hello</H1><img src='cid:image'>"; messageBodyPart.setContent(htmlText, "text/html"); // add it multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource("D:\\java.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID", "<image>"); // add image to the multipart multipart.addBodyPart(messageBodyPart); // put everything together message.setContent(multipart); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } } }
效果图
3.带附件的邮件
代码
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.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; 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; /** * 发送带附件的邮件 * * @author Administrator * */ public class emailZip { public static void main(String[] args) { // Recipient's email ID needs to be mentioned. String to = "978108040@qq.com"; // Sender's email ID needs to be mentioned String from = "13155903710@163.com"; final String username = "13155903710@163.com";// change accordingly final String password = "z1234567";//授权密码非邮箱登录密码 // Assuming you are sending email through relay.jangosmtp.net String host = "relay.jangosmtp.net"; Properties props = new Properties(); // 开启debug调试 props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "true"); // 设置邮件服务器主机名 props.setProperty("mail.host", "smtp.163.com"); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); // Get the Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Testing Subject"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Now set the actual message messageBodyPart.setText("This is message body"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "D://1.ZIP"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } } }
效果图
源码链接–>java实现发送邮件 下载,这三个功能开发中基本上够用了
相关文章推荐
- 强大的java编程实现邮件发送功能,支持内容,上传多个图片和多个附件
- java实现发送邮件可多个附件内嵌图片-commons-email使用
- java实现发送邮件可多个附件内嵌图片-commons-email使用(未测试)
- JavaMail实现发送邮件,可添加附件(JAVA SWING)
- java实现发送邮件包含添加附件等(一)
- Java-发送邮件(附件、图片)---(五)实例
- java程序发送内嵌图片、带附件邮件
- java实现邮件抄送,密送,多个附件发送
- Java-发送邮件(附件、图片)---(四)类-MimeMultipart、MimeBodyPart、FileDataSource、DataHandler、
- 用java实现的邮件系统六(javamail发送复杂邮件-带附件和html)
- java带图片的邮件发送方法实现
- Java实现发送邮件功能(可带附件)
- java发送邮件,正文图片,附件
- Java-发送邮件(附件、图片)---(三)类-Properties、Session、MimeMessage
- java邮件发送 实现群发和带附件发送
- Spring实现邮件发送之添加附件、内嵌图片
- java带图片的邮件发送方法实现
- java中javamail发送带附件的邮件实现方法
- JavaMail实现邮件发送的简单封装(包括文本、图片、html、附件)
- java发送邮件带附件、格式、可以实现转发、秘密转发等操作