📄 sendemail.java
字号:
package tools.util;
/**
* Created by zywang
* Date: 2007-2-7
* Time: 20:56:58
*/
import javax.mail.internet.InternetAddress;
import java.util.Date;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.io.UnsupportedEncodingException;
import javax.mail.*;
/**
* 发送Email类
*/
public class SendEmail {
public SendEmail() {
}
public void send(String toAddress) {
try {
Properties props = new Properties();
Session sendMailSession;
Transport transport;
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com"); //smtp主机名。
// props.put("mail.from", "合肥吃吧<eatba@163.com>"); //发送人姓名。
// props.put("mail.smtp.user", "合肥吃吧"); //发送方邮件地址。
// props.put("mail.smtp.password", "05568925952"); //邮件密码。
PopupAuthenticator popA = new PopupAuthenticator();//邮件安全认证。
popA.performCheck("eatba", "05568925952"); //填写用户名及密码
sendMailSession = Session.getInstance(props, popA);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setDescription("合肥吃吧");
newMessage.setFrom(new InternetAddress("eatba@163.com"));
newMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(toAddress)); //接收方邮件地址
String subject="邮件主题subject";
String tmp = new String(subject.getBytes("GBK"));
newMessage.setSubject(tmp);
newMessage.setSentDate(new Date());
String content="邮件正文";
newMessage.setText(new String(content.getBytes("GBK"))); //邮件正文
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
} catch (MessagingException ex) {
ex.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public class PopupAuthenticator extends Authenticator {
String username = null;
String password = null;
public PopupAuthenticator() {
}
public PasswordAuthentication performCheck(String user, String pass) {
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
public boolean isEmail(String str){
str = str.trim();
String regex = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
return str.matches(regex);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -