Properties prop=new Properties();
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.transport.protocol", "smtp");
Session session=Session.getInstance(prop);
session.setDebug(true);
Message msg=new MimeMessage(session);
msg.setText("hello jmail!");
msg.setFrom(new InternetAddress("laiwei"));
Transport trans=session.getTransport();
trans.connect("smtp.sohu.com", 25, "aloyise", "135129");
trans.sendMessage(msg, new Address[]{new InternetAddress("[email protected]")});
trans.close();
账号是我现注册的,报的错是com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.1 Command rejected
我查了下说是地址不正确才会报的错,但是我搞不明白,换了好几个地址都不行,请达人指教
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.transport.protocol", "smtp");
Session session=Session.getInstance(prop);
session.setDebug(true);
Message msg=new MimeMessage(session);
msg.setText("hello jmail!");
msg.setFrom(new InternetAddress("laiwei"));
Transport trans=session.getTransport();
trans.connect("smtp.sohu.com", 25, "aloyise", "135129");
trans.sendMessage(msg, new Address[]{new InternetAddress("[email protected]")});
trans.close();
账号是我现注册的,报的错是com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.1 Command rejected
我查了下说是地址不正确才会报的错,但是我搞不明白,换了好几个地址都不行,请达人指教
javamail 用法有两种:1.不需要验证
jsp版本<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%@ page import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*" %>
<html>
<head>
<TITLE>JavaMail 电子邮件发送系统</TITLE>
</HEAD>
<BODY><%
try{ Properties props = new Properties();
Session sendMailSession = Session.getInstance(props, null);
Message newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress("<[email protected]>"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("<[email protected]>"));
newMessage.setSubject("subject21");
newMessage.setSentDate(new Date()); newMessage.setText("text21"); Transport transport = sendMailSession.getTransport("smtp");
transport.connect("smtp.163.com", "tj_yangyongfu", "11111111111111");
newMessage.saveChanges();
transport.sendMessage(newMessage, newMessage.getAllRecipients());
transport.close();%><p>你的邮件已发送,请返回。</p><% }
catch(Exception e){
e.printStackTrace();
out.println(e.toString());
}%></BODY>
</HTML>
2.需要验证 此时又有两种写法
2.1
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%@ page import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*" %>
<html>
<head>
<TITLE>JavaMail 电子邮件发送系统</TITLE>
</HEAD>
<BODY><%
try{ Properties props = new Properties();
props.put("mail.smtp.auth","true");
Session sendMailSession = Session.getInstance(props, null);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("[email protected]"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
newMessage.setSubject("subject21");
newMessage.setSentDate(new Date());
newMessage.setText("text21");
Transport transport = sendMailSession.getTransport("smtp");
transport.connect("smtp.163.com", "[email protected]", "");
newMessage.saveChanges();
transport.sendMessage(newMessage, newMessage.getAllRecipients());
transport.close();%>
<p>你的邮件已发送,请返回。</p>
<%
}
catch(Exception e){
e.printStackTrace();
out.println(e.toString());
}
%></BODY>
</HTML>2.2<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%@ page import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*" %>
<html>
<head>
<TITLE>JavaMail 电子邮件发送系统</TITLE>
</HEAD>
<BODY><%
try{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth","true");
Session sendMailSession = Session.getInstance(props,
new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
String username = "[email protected]"; //163邮箱登录帐号
String pwd = " "; //登录密码
return new PasswordAuthentication(username, pwd);
}
}
); Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("<[email protected]>"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("<[email protected]>"));
newMessage.setSubject("subject21");
newMessage.setSentDate(new Date());
newMessage.setText("text21");
Transport .send(newMessage);
%><p>你的邮件已发送,请返回。</p><%
}
catch(Exception e){
e.printStackTrace();
out.println(e.toString());
}%>
</BODY>
</HTML>