Need smtp authentication. props.put("mail.host", host); props.put("mail.transport.protocol", "smtp"); //authentication is needed props.put("mail.smtp.auth", true); Transport transport; transport = mailSession.getTransport("smtp"); transport.send(msg); Tansport.send(msg); //this code is enough.
不行啊,我加上props.put("mail.smtp.auth", true); 后还是出错 org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: -1 in the jsp file: nullGenerated servlet error: [javac] Since fork is true, ignoring compiler setting. [javac] Compiling 1 source file [javac] Since fork is true, ignoring compiler setting. [javac] D:\Tomcat\work\Standalone\localhost\_\mail_jsp.java:71: cannot resolve symbol [javac] symbol : method put (java.lang.String,boolean) [javac] location: class java.util.Properties [javac] props.put("mail.smtp.auth",true) ; [javac] ^ [javac] 1 error
// Get system properties Properties props = new Properties(); // Setup mail server props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); //这样才能通过验证 // Get session Session session = Session.getDefaultInstance(props); // watch the mail commands go by to the mail server session.setDebug(true); // Define message MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); message.setSubject(subject); message.setText(content);
props.put("mail.transport.protocol", "smtp");
//authentication is needed
props.put("mail.smtp.auth", true);
Transport transport;
transport = mailSession.getTransport("smtp");
transport.send(msg);
Tansport.send(msg); //this code is enough.
后还是出错
org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: -1 in the jsp file: nullGenerated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] D:\Tomcat\work\Standalone\localhost\_\mail_jsp.java:71: cannot resolve symbol
[javac] symbol : method put (java.lang.String,boolean)
[javac] location: class java.util.Properties
[javac] props.put("mail.smtp.auth",true) ;
[javac] ^
[javac] 1 error
我调试成功的代码如下:
你试试看:
package bean;
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class sendMail
{ String host ="";
String from="";
String to="";
String subject="";
String content="";
String username ="";
String password ="";
String fileAttachment="";
public void setFrom(String s)
{
from = s;
} public void setTo(String s)
{
to= s;
} public void setSubject(String s)
{
subject = s;
}
public void setContent(String s)
{
content= s;
}
public void setFileAttachment(String s)
{
fileAttachment= s;
}
public void send ()throws Exception
{ host = "smtp.163.com";
from = "[email protected]";
to = "[email protected]";
username = "test";
password = "test";
subject = "你好";
content = "测试JAVAMAIL";
fileAttachment = "C:\\12.gif";
// Get system properties
Properties props = new Properties(); // Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true"); //这样才能通过验证 // Get session
Session session = Session.getDefaultInstance(props); // watch the mail commands go by to the mail server
session.setDebug(true); // Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
message.setText(content);
// 第二部分信息
MimeBodyPart mbp2 = new MimeBodyPart(); // 在第二部分信息中附加一个文件
FileDataSource fds = new FileDataSource( fileAttachment );
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName()); // 创建 Multipart 并放入每个 MimeBodyPart
Multipart mp = new MimeMultipart();
mp.addBodyPart( mbp2 ); // 增加 Multipart 到信息体
message.setContent( mp ); // Send message
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
props.put("mail.smtp.auth", "true");不是
props.put("mail.smtp.auth", true);
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
);
控制面版-->系统-->高级-->环境变量-->系统变量-->classpath
添加相应的.jar路径,好象有mail.jar,smtp.jar,pop3.jar等等