<%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
contentType="text/html; charset=gb2312"%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<%
//------------------------------
class MyAuthenticator
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
} protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}//-----------------------------
String smtphost = "smtp.126.com";
String from = "[email protected]";
String to = "[email protected]";
String subject = "JavaMail 测试邮件";
String content = "Hello world!";
try{
Properties props = new Properties();
Session sendMailSession;
Transport transport;
MyAuthenticator myauth = new MyAuthenticator("邮箱用户名","邮箱密码");
sendMailSession = Session.getInstance(props, myauth);
props.put("mail.smtp.host",smtphost);
props.put("mail.smtp.auth","true"); //这样才能通过验证Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
newMessage.setText(content);
newMessage.saveChanges();
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>
</HTML>
--------------------------------------------现在的问题是 这个是我自己的邮箱知道用户名和密码但是发给别人呢?不验证就不能发了吗?
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
contentType="text/html; charset=gb2312"%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<%
//------------------------------
class MyAuthenticator
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
} protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}//-----------------------------
String smtphost = "smtp.126.com";
String from = "[email protected]";
String to = "[email protected]";
String subject = "JavaMail 测试邮件";
String content = "Hello world!";
try{
Properties props = new Properties();
Session sendMailSession;
Transport transport;
MyAuthenticator myauth = new MyAuthenticator("邮箱用户名","邮箱密码");
sendMailSession = Session.getInstance(props, myauth);
props.put("mail.smtp.host",smtphost);
props.put("mail.smtp.auth","true"); //这样才能通过验证Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
newMessage.setText(content);
newMessage.saveChanges();
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>
</HTML>
--------------------------------------------现在的问题是 这个是我自己的邮箱知道用户名和密码但是发给别人呢?不验证就不能发了吗?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货