javamail问题,请各位帮我调通它,很简单,编译就能用,一个测试javamail的application,100分送上,绝不食言

snowman_pc 2004-10-29 12:37:39
import javax.mail.*;
import javax.activation.*;
import java.util.*;
import javax.mail.internet.*;

public class SendMail
{
public static void main(String[] args)
{
try
{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
props.put("mail.smtp.host", "smtp.163.com");
//props.put("mail.smtp.auth","true");
// props.put("mail.smtp.user", "jb_china2004");
//props.put("mail.smtp.password", "不能说");
sendMailSession = Session.getInstance(props, null);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("jb_china2004@163.com"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("jb_china2008@163.com"));
newMessage.setSubject("测试发信");
newMessage.setSentDate(new Date());
newMessage.setText("测试发信成功,欢喜之中");
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

...全文
225 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zenozjs 2004-12-14
  • 打赏
  • 举报
回复
虽然已经结贴了,我还是要来拜一下chinajava
chinajava 2004-11-01
  • 打赏
  • 举报
回复
import javax.mail.*;
import javax.activation.*;
import java.util.*;
import javax.mail.internet.*;

public class SendMail
{
public static void main(String[] args)
{
try
{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
SmtpAuthenticator sa=new SmtpAuthenticator("jb_china2004","jbChina2004");
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth","true");
// props.put("mail.smtp.user", "jb_china2004");
// props.put("mail.smtp.password", "jbChina2004");
// sendMailSession = Session.getInstance(props, null);
sendMailSession = Session.getInstance(props, sa);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("jb_china2004@163.com"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("jb_china2008@163.com"));
newMessage.setSubject("测试发信");
newMessage.setSentDate(new Date());
newMessage.setText("测试发信成功,欢喜之中");
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
System.out.println ("发送成功");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class SmtpAuthenticator extends Authenticator
{

String username;
String password;

public SmtpAuthenticator(String s, String s1)
{
username = s;
password = s1;
}

protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}
snowman_pc 2004-10-31
  • 打赏
  • 举报
回复
顶,没有人帮忙吗?
snowman_pc 2004-10-29
  • 打赏
  • 举报
回复
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MyAuthenticator extends Authenticator
{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public MyAuthenticator (String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username, m_userpass);
}
}




import javax.mail.*;
import javax.activation.*;
import java.util.*;
import javax.mail.internet.*;

public class SendMail
{
public static void main(String[] args)
{
try
{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
props.put("mail.smtp.host", "smtp.163.com");

props.put("mail.smtp.user", "jb_china2004");
props.put("mail.smtp.password", "jbChina2004");
//props.put("mail.smtp.auth","true");
//sendMailSession = Session.getInstance(props, null);
sendMailSession = Session.getInstance(props, new MyAuthenticator("jb_china2004","jbChina2004"));
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("jb_china2004@163.com"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("jb_china2008@163.com"));
newMessage.setSubject("测试发信");
newMessage.setSentDate(new Date());
newMessage.setText("测试发信成功,欢喜之中");
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
System.out.println ("发送成功");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

还是不好用,
提示
javax.mail.MessagingException:553 you are not authorized to send mail as<mail from <jb_china2004@163.com>>,authentication is required

密码也告诉你啦, 麻烦你帮忙看一下呸,我没做过邮件发送, 都好几天没弄出来啦!
stonecsdn 2004-10-29
  • 打赏
  • 举报
回复
//props.put("mail.smtp.auth","true");
// props.put("mail.smtp.user", "jb_china2004");
//props.put("mail.smtp.password", "不能说");
把注释去掉,写一个授权类:
public class MyAuthenticator extends javax.mail.Authenticator
{
private String m_username = null;
private String m_userpass = null;

public void setUsername(String username)
{
m_username = username;
}

public void setUserpass(String userpass)
{
m_userpass = userpass;
}

public MyAuthenticator (String username, String userpass)
{
super();

setUsername(username);
setUserpass(userpass);

}
public PasswordAuthentication getPasswordAuthentication()
{

return new PasswordAuthentication(m_username,m_userpass);
}
}
//***********************
sendMailSession = Session.getInstance(props, new MyAuthenticator(username,password));

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧