使用java发送邮件,这里发件是使用qq邮箱发送;如果使用其他类型邮箱,将qq相关的信息改成对应的邮箱;
import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import com.sun.mail.util.MailSSLSocketFactory; public class JavaMailDemo { public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty("mail.host", "smtp.qq.com"); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.smtp.auth", "true"); //开启SSL加密, MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable", "true"); prop.put("mail.smtp.ssl.socketFactory", sf); //1、创建Session Session session = Session.getInstance(prop); //开启Sessiondebug模式,查询程序发送Email的运行状态 session.setDebug(true); //通过session得到transport对象 Transport transport = session.getTransport(); //使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器, //用户名和密码都通过验证之后才能够发送邮件给收件人 //"xxx"是qq邮箱授权码,可以百度qq邮箱授权码是如何获得的,将这个“xxx”换成授权码即可 transport.connect("smtp.qq.com","[email protected]","xxx"); //创建简单文本邮件 //Message message = createSimpleMail(session); Message message = createAttchMail(session);//创建带附件的邮件 //发送邮件 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } /** * 发送简单文本邮件 * @param session * @return * @throws AddressException * @throws MessagingException */ public static MimeMessage createSimpleMail(Session session) throws AddressException, MessagingException{ //创建邮件对象 MimeMessage message = new MimeMessage(session); //指明邮件的发件人 message.setFrom(new InternetAddress("[email protected]")); message.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]")); //邮件的标题 message.setSubject("只包含文本的简单邮件"); //邮件的文本内容 message.setContent("你好啊","text/html;charset=utf-8"); return message; } /** * 发送带附件的邮件 * @throws MessagingException * @throws AddressException */ public static MimeMessage createAttchMail(Session session) throws AddressException, MessagingException{ //创建邮件对象 MimeMessage message = new MimeMessage(session); //指明邮件的发件人 message.setFrom(new InternetAddress("[email protected]")); //message.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]")); //批量发送邮件 Address[] addresses = new Address[2]; addresses[0] = new InternetAddress("[email protected]"); addresses[1] = new InternetAddress("[email protected]"); message.setRecipients(Message.RecipientType.TO, addresses); //邮件标题 message.setSubject("发送带附件的邮件"); //创建邮件正文,设置编码格式 MimeBodyPart text = new MimeBodyPart(); text.setContent("使用javamail创建带附件的邮件", "text/html;charset=utf-8"); //创建邮件附件 MimeBodyPart attach = new MimeBodyPart(); File file = new File("F:/学位.jpg"); DataHandler dh = new DataHandler(new FileDataSource(file)); attach.setDataHandler(dh); attach.setFileName(dh.getName()); //创建容器描述数据关系 MimeMultipart mp = new MimeMultipart(); mp.addBodyPart(text); mp.addBodyPart(attach); mp.setSubType("mixed"); message.setContent(mp); message.saveChanges(); return message; } }
原文地址:https://www.cnblogs.com/lazyli/p/10841645.html
时间: 2024-11-09 00:05:16