<%@ 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>
--------------------------------------------现在的问题是 这个是我自己的邮箱知道用户名和密码但是发给别人呢?不验证就不能发了吗?

解决方案 »

  1.   

    楼上的仁兄那么有什么方法可以到别人的邮箱不然这个javamail失去意义了
      

  2.   

    发件人填用户名和密码,不是收件人我发邮件给你,需要我的email、密码,而只需要你的email,明白?
      

  3.   

    hehe 我糊涂了谢谢兄弟提醒 哈哈那问题比较好解决了只要邮件服务器密码验证就OK了