当前位置: 技术问答>java相关
关于javamail.
来源: 互联网 发布时间:2015-06-27
本文导语: 怎样用javamail实现smtp服务器的发送认证。 谢谢! | props.put("mail.smtp.auth", "true"); props.put("mail.smtp.user", userId); props.put("mail.smtp.password",password); session = javax.ma...
怎样用javamail实现smtp服务器的发送认证。
谢谢!
谢谢!
|
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", userId);
props.put("mail.smtp.password",password);
session = javax.mail.Session.getInstance(props,new MyAuthenticator(userId, password));
其中的MyAuthenticator
import javax.mail.*;
public class MyAuthenticator extends Authenticator{
String userId;
String password;
public MyAuthenticator(String userId, String password){
this.userId = userId;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userId, password);
}
}
props.put("mail.smtp.user", userId);
props.put("mail.smtp.password",password);
session = javax.mail.Session.getInstance(props,new MyAuthenticator(userId, password));
其中的MyAuthenticator
import javax.mail.*;
public class MyAuthenticator extends Authenticator{
String userId;
String password;
public MyAuthenticator(String userId, String password){
this.userId = userId;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userId, password);
}
}
|
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class sendMail
{
public static void main(String args[]) throws Exception
{
String host = "smtp.sina.com.cn";
String from = "javamail@sina.com";
String to = "javamail@china.com";
String username = "javamail";
String password = "password";
// Get system properties
// Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
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("Hello JavaMail");
message.setText("Welcome to JavaMail");
// Send message
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
import javax.mail.internet.*;
import java.util.*;
public class sendMail
{
public static void main(String args[]) throws Exception
{
String host = "smtp.sina.com.cn";
String from = "javamail@sina.com";
String to = "javamail@china.com";
String username = "javamail";
String password = "password";
// Get system properties
// Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
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("Hello JavaMail");
message.setText("Welcome to JavaMail");
// Send message
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。