送一段代码给你,自己看看,应该不会错的。
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.util.Properties;
import javax.servlet.http.*;
import javax.activation.*;public class javaMailSend {
private Exception exception = null; //记录异常 public javaMailSend() {
} public void setException(Exception exception) {
this.exception = exception;
} public Exception getException() {
return this.exception;
} public boolean SendMail(String from, String recipients, String submit,
String body) throws UserMailException {
boolean isSend = false;
try {
Properties propes = new Properties();
propes.put("mail.transport.protocol", "smtp"); //发送邮件的协议
propes.put("mail.smtp.host", "smtp.126.com"); //发送邮件的服务器
propes.put("mail.smtp.port", "25"); //服务器的端口号
propes.put("mail.smtp.auth", "true"); //如果smtp服务器要求验证需使用该语句
System.setProperty("mail.mime.charset", "gbk"); //邮件编码设置 MyAuthenticator auth = new MyAuthenticator("accp_developer",
"effort");
Session mailsession = Session.getInstance(propes, auth);
MimeMessage msg = new MimeMessage(mailsession); msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipients));
msg.setSentDate(new Date());
msg.setSubject(submit);
msg.setText(body); Transport trans = mailsession.getTransport();
trans.send(msg);
isSend = true;
} catch (Exception ex) {
this.setException(ex); //写入异常
ex.printStackTrace();
System.out.println(ex);
}
return isSend;
} /**
* 密码认证类, 如果SMTP与POP需要身份验证即通过该類來進行驗證。
*/
private class MyAuthenticator extends Authenticator {
private PasswordAuthentication pwAuth; public MyAuthenticator(String user, String passwd) {
pwAuth = new PasswordAuthentication(user, passwd);
} protected PasswordAuthentication getPasswordAuthentication() {
return pwAuth;
}
}
}
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.util.Properties;
import javax.servlet.http.*;
import javax.activation.*;public class javaMailSend {
private Exception exception = null; //记录异常 public javaMailSend() {
} public void setException(Exception exception) {
this.exception = exception;
} public Exception getException() {
return this.exception;
} public boolean SendMail(String from, String recipients, String submit,
String body) throws UserMailException {
boolean isSend = false;
try {
Properties propes = new Properties();
propes.put("mail.transport.protocol", "smtp"); //发送邮件的协议
propes.put("mail.smtp.host", "smtp.126.com"); //发送邮件的服务器
propes.put("mail.smtp.port", "25"); //服务器的端口号
propes.put("mail.smtp.auth", "true"); //如果smtp服务器要求验证需使用该语句
System.setProperty("mail.mime.charset", "gbk"); //邮件编码设置 MyAuthenticator auth = new MyAuthenticator("accp_developer",
"effort");
Session mailsession = Session.getInstance(propes, auth);
MimeMessage msg = new MimeMessage(mailsession); msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipients));
msg.setSentDate(new Date());
msg.setSubject(submit);
msg.setText(body); Transport trans = mailsession.getTransport();
trans.send(msg);
isSend = true;
} catch (Exception ex) {
this.setException(ex); //写入异常
ex.printStackTrace();
System.out.println(ex);
}
return isSend;
} /**
* 密码认证类, 如果SMTP与POP需要身份验证即通过该類來進行驗證。
*/
private class MyAuthenticator extends Authenticator {
private PasswordAuthentication pwAuth; public MyAuthenticator(String user, String passwd) {
pwAuth = new PasswordAuthentication(user, passwd);
} protected PasswordAuthentication getPasswordAuthentication() {
return pwAuth;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货