当前位置: 技术问答>java相关
请看我的JavaMail为何不能发邮件?有代码。
来源: 互联网 发布时间:2017-04-02
本文导语: import java.util.*; import javax.mail.internet.*; import javax.mail.*; public class MailSender1 { public static void main(String[] args) throws Exception { String smtp = "smtp.163.com"; String from = "zhangjun1778@sina.com";//我的邮箱 String to = "zhan...
import java.util.*;
import javax.mail.internet.*;
import javax.mail.*;
public class MailSender1
{
public static void main(String[] args) throws Exception
{
String smtp = "smtp.163.com";
String from = "zhangjun1778@sina.com";//我的邮箱
String to = "zhangjun1778@sina.com";//我的邮箱
Properties props = System.getProperties();
Session session = Session.getInstance(props,null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
msg.setSubject("Send mail 测试");
msg.setText(" Test text 测试");
Transport transport =session.getTransport("smtp");
transport.connect(smtp,"","");//后两个参数如何添?
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
}
}
//error:javax.mail.MessagingException:550:Invalid User
import javax.mail.internet.*;
import javax.mail.*;
public class MailSender1
{
public static void main(String[] args) throws Exception
{
String smtp = "smtp.163.com";
String from = "zhangjun1778@sina.com";//我的邮箱
String to = "zhangjun1778@sina.com";//我的邮箱
Properties props = System.getProperties();
Session session = Session.getInstance(props,null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
msg.setSubject("Send mail 测试");
msg.setText(" Test text 测试");
Transport transport =session.getTransport("smtp");
transport.connect(smtp,"","");//后两个参数如何添?
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
}
}
//error:javax.mail.MessagingException:550:Invalid User
|
下面是我基于sun的tech tip中的例子改写来的,带身份验证。
请把username和password换了就OK
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SendingMail {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.neusoft.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new MailAuthenticator());
MimeMessage message = new MimeMessage(session);
Address address = new InternetAddress("xxx@xxx.com");
message.setFrom(address);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("xxx@xxx.com"));
message.setSubject("Hello, JDC");
message.setText("Welcome to the JDC");
Address[] addressArray = {
address
};
Transport.send(message);
}
static class MailAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
}
请把username和password换了就OK
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SendingMail {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.neusoft.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new MailAuthenticator());
MimeMessage message = new MimeMessage(session);
Address address = new InternetAddress("xxx@xxx.com");
message.setFrom(address);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("xxx@xxx.com"));
message.setSubject("Hello, JDC");
message.setText("Welcome to the JDC");
Address[] addressArray = {
address
};
Transport.send(message);
}
static class MailAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。