使用javaMail发送邮件
2017-08-18 15:33
375 查看
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 需要mail.jar
* 需要对发送邮箱开启POP3/SMTP服务,并保存授权密码
* Created by hgc on 2017/8/18.
*/
public class AutoboxingAndUnboxing{
public static void main(String[] args) throws Exception {
String to =”xxx@qq.com”;// 收件人电子邮箱
String from = “xxxx@qq.com”;// 发件人电子邮箱
String host = “smtp.qq.com”;//指定发送邮件的主机为 smtp.qq.com,该主机为QQ 邮件服务器
Properties properties = System.getProperties(); // 获取系统属性
properties.setProperty(“mail.smtp.host”,host);// 设置邮件服务器
/**
* 关于QQ邮箱,还要设置SSL加密,加上以下代码即可
*/
properties.put(“mail.smtp.auth”, “true”);
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put(“mail.smtp.ssl.enable”, “true”);
properties.put(“mail.smtp.ssl.socketFactory”, sf);
}
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 需要mail.jar
* 需要对发送邮箱开启POP3/SMTP服务,并保存授权密码
* Created by hgc on 2017/8/18.
*/
public class AutoboxingAndUnboxing{
public static void main(String[] args) throws Exception {
String to =”xxx@qq.com”;// 收件人电子邮箱
String from = “xxxx@qq.com”;// 发件人电子邮箱
String host = “smtp.qq.com”;//指定发送邮件的主机为 smtp.qq.com,该主机为QQ 邮件服务器
Properties properties = System.getProperties(); // 获取系统属性
properties.setProperty(“mail.smtp.host”,host);// 设置邮件服务器
/**
* 关于QQ邮箱,还要设置SSL加密,加上以下代码即可
*/
properties.put(“mail.smtp.auth”, “true”);
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put(“mail.smtp.ssl.enable”, “true”);
properties.put(“mail.smtp.ssl.socketFactory”, sf);
Session session = Session.getDefaultInstance(properties,new Authenticator(){ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication("xxx@qq.com", "password"); //发件人邮件用户名、密码,使用的是授权密码 } }); MimeMessage message = new MimeMessage(session);// 创建默认的 MimeMessage 对象 message.setFrom(new InternetAddress(from));// Set from: 头部头字段(正常邮箱发送中的发件人) message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));// Set To: 头部头字段(正常邮箱发送中的收件人) message.setSubject("This is the Subject Line!");// Set Subject: 头部头字段(正常邮箱发送的主题) message.setText("This is actual message"); // 设置消息体(正常邮箱发送的正文) Transport.send(message);// 发送消息 System.out.println("Sent message successfully...."); }
}
相关文章推荐
- Java Web(十三) 使用javamail进行发送邮件,(使用QQ,163,新浪邮箱服务器)
- 使用JavaMail发送邮件
- tomcat使用javamail发送邮件失败解决办法
- android 随手记 android使用javamail发送邮件
- WEB项目使用JavaMail 发送邮件
- 使用JavaMail发送邮件保证成功的方法
- 使用JavaMail发送邮件和接受邮件
- Android 使用javaMail jar包 发送邮件
- android中使用javamail发送邮件附件
- 使用JavaMail发送邮件
- 关于Java Web 使用JavaMail创建邮件和发送邮件
- 使用JavaMail发送邮件
- 使用javaMail发送复杂邮件,支持附件发送
- 使用JavaMail创建邮件和发送邮件
- 使用JavaMail发送邮件
- 使用JavaMail SMTP协议发送邮件
- javamail 发送网页形式的邮件 使用后的一点感想
- JavaWeb应用中使用JavaMail发送邮件
- 使用JavaMail创建邮件和发送邮件
- 使用JavaMail发送邮件,并使用465端口开启ssl加密传输,保证邮件安全