package com.statestr.ssc;
import java.util.Date;
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
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;
/**
*
* @author AjunBoys
*
*/
public class Email {
private String host;
private String username;
private String password;
private String from;
private String to;
private String subject;
private String content;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Email(){
this.host = "smtp.126.com";
this.username = "wlq0215";
this.password = "******";
this.from = "wlq0215@126.com";
}
public static void main(String[] args) {
Email email = new Email();
email.setContent("hello");
email.sendMail("wlq0215@126.com", "****");
}
//发送邮件
public boolean sendMail(String from,String to){
try{
Properties props = new Properties();//存储连接参数
props.put("mail.smtp.host", host); //smtp主机
props.put("mail.smtp.auth", "true"); //需要身份验证
Session session = Session.getInstance(props);
//获取一个邮件session
session.setDebug(true);
MimeMessage message = new MimeMessage(session); //邮件会话新建一个邮件信息
//检查邮件地址是否合法
if(from == null || from ==""){
throw new Exception("Error Email Address in From.");
}
if(to == null || to ==""){
throw new Exception("Error Email Address in To.");
}
//设置源地址
//message.setFrom(new InternetAddress(from)); //等价于下面两条代码
InternetAddress addressFrom = new InternetAddress(from);
message.setFrom(addressFrom);
//设置目的地址
InternetAddress addressTo = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, addressTo);
message.setSubject(subject);
//message.setText(content); //邮件正文
Multipart mp = new MimeMultipart();//邮件内容
MimeBodyPart mbpContent = new MimeBodyPart();
mbpContent.setContent(content,"text/html"); //邮件格式
//向MimeMessage添加代表正文(Multipart)
mp.addBodyPart(mbpContent);
message.setContent(mp);
message.setSentDate(new Date()); //设置发送的时间
Transport transport = session.getTransport("smtp"); //设置邮件传输
transport.connect((String)props.get("mail.smtp.host"), username, password); //连接主机
transport.sendMessage(message, message.getRecipients(MimeMessage.RecipientType.TO)); //发送邮件
System.out.println("Send Success !");
transport.close(); //关闭传输
return true;
}catch(AuthenticationFailedException e){
e.printStackTrace();
return false;
} catch(MessagingException e){
e.printStackTrace();
return false;
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
分享到:
相关推荐
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication ...
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); // 或其他端口,如25、465 props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); /...
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); // 创建Session对象 Session session = Session.getInstance(props, new ...
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new Authenticator() { protected ...
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("receiverEmail@example.com")); message.setSubject("Test Email"); message.setText("Hello, this is a test email sent from Java!")...
properties.put("mail.smtp.host", "smtp.example.com"); properties.put("mail.smtp.port", 587); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); ``` 接下来...
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new Authenticator() { protected ...
props.put("mail.smtp.host", "smtp.example.com"); Session session = Session.getDefaultInstance(props);` 2. 创建邮件消息:`Message message = new MimeMessage(session);` 3. 设置发件人和收件人:`message....
properties.put("mail.smtp.host", "smtp.example.com"); properties.put("mail.smtp.port", "587"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); Session ...
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); Authenticator auth = new ...
props.put("mail.smtp.host", "smtp.example.com"); Session session = Session.getDefaultInstance(props); Message message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@...
import javax.mail.util.BASE64EncoderStream; import java.io.*; public class Base64Example { public static void main(String[] args) throws IOException { // 编码 byte[] data = "Hello, World!"....
props.put("mail.smtp.host", "smtp.example.com"); Session session = Session.getInstance(props); Message message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")...
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); // 创建Session对象 Session session = Session.getInstance(props, new ...
properties.setProperty("mail.pop3.host", "pop3.example.com"); properties.setProperty("mail.pop3.port", "110"); ``` 2. **创建Session对象**:使用Properties对象创建一个Session实例,这是所有邮件操作的...
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); // 创建会话并登录SMTP服务器 Session session = Session.getInstance(props, new Authenticator() { protected ...
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); // 创建 Session 对象 Session session = Session.getInstance(props, new ...
props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new Authenticator() { protected ...
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email@example.com")); message.setSubject("邮件主题"); message.setText("邮件内容"); ``` 如果邮件需要包含附件,可以使用`...