当前位置: 技术问答>java相关
关于javamail的小问题,解决力送100分!!!(jar打包问题的分已结)
来源: 互联网 发布时间:2015-02-19
本文导语: 我用javamail发邮件,需要做password Authenticator 即若不做验证则 Session session = Session.getDefaultInstance(System.getProperties(),null); 若要做验证则 Session session = Session.getDefaultInstance(System.getProperties(),auth); 其中,auth实现抽象类A...
我用javamail发邮件,需要做password Authenticator
即若不做验证则
Session session = Session.getDefaultInstance(System.getProperties(),null);
若要做验证则
Session session = Session.getDefaultInstance(System.getProperties(),auth);
其中,auth实现抽象类Authenticator
但该验证该如何做呢,我还是不知道,请教!!
另:下面的错误是什么原因呢
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
javax.mail.SendFailedException: 550 Relaying is prohibited
java.lang.Throwable(java.lang.String)
java.lang.Exception(java.lang.String)
javax.mail.MessagingException(java.lang.String, java.lang.Exception)
javax.mail.SendFailedException(java.lang.String, java.lang.Exception, javax.mail.Address [], javax.mail.Address [], javax.mail.Address [])
void javax.mail.Transport.send0(javax.mail.Message, javax.mail.Address [])
void javax.mail.Transport.send(javax.mail.Message)
boolean com.eastpro.hkisc.util.HtmlMail.send()
boolean com.eastpro.hkisc.bizlogic.GBMailBean.sendOneMail(com.eastpro.hkisc.bizlogic.MailDetail)
void com.eastpro.hkisc.bizlogic.GBMailBean.sendAllMail()
void com.eastpro.hkisc.bizlogic.GBMailPostMan.run()
即若不做验证则
Session session = Session.getDefaultInstance(System.getProperties(),null);
若要做验证则
Session session = Session.getDefaultInstance(System.getProperties(),auth);
其中,auth实现抽象类Authenticator
但该验证该如何做呢,我还是不知道,请教!!
另:下面的错误是什么原因呢
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
javax.mail.SendFailedException: 550 Relaying is prohibited
java.lang.Throwable(java.lang.String)
java.lang.Exception(java.lang.String)
javax.mail.MessagingException(java.lang.String, java.lang.Exception)
javax.mail.SendFailedException(java.lang.String, java.lang.Exception, javax.mail.Address [], javax.mail.Address [], javax.mail.Address [])
void javax.mail.Transport.send0(javax.mail.Message, javax.mail.Address [])
void javax.mail.Transport.send(javax.mail.Message)
boolean com.eastpro.hkisc.util.HtmlMail.send()
boolean com.eastpro.hkisc.bizlogic.GBMailBean.sendOneMail(com.eastpro.hkisc.bizlogic.MailDetail)
void com.eastpro.hkisc.bizlogic.GBMailBean.sendAllMail()
void com.eastpro.hkisc.bizlogic.GBMailPostMan.run()
|
这个是已经通过的.
public void sendMail(){
String to ="xxx@163.com";
String from ="xxxx@163.net";
String server="smtp.163.net";
String user="xxx";
String password="xxxxxxx";
try{
Properties props=new Properties ();
props.put("mail.smtp.host",server);
props.put("mail.smtp.auth","true");
props.put("mail.bebug","true");
//URLName urlName=new URLName(server);
//PasswordAuthentication pa=new PasswordAuthentication(user,password);
Session sendMailSession=Session.getDefaultInstance(props,null);
sendMailSession.setDebug(true);
//sendMailSession.setPasswordAuthentication (urlName,pa);
//SMTPTransport transport=new SMTPTransport(sendMailSession,null);
Transport transport=sendMailSession.getTransport("smtp");
Message msg = new MimeMessage(sendMailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Java Mail Test");
msg.setHeader("X-Mailer", "msgsend");
msg.setSentDate(new Date());
//msg.setText ("Lalalala......");
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("Lalalala.......");
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource("c://msdos.sys");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
transport.connect (server,user,password);
//transport.connect ();
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void sendMail(){
String to ="xxx@163.com";
String from ="xxxx@163.net";
String server="smtp.163.net";
String user="xxx";
String password="xxxxxxx";
try{
Properties props=new Properties ();
props.put("mail.smtp.host",server);
props.put("mail.smtp.auth","true");
props.put("mail.bebug","true");
//URLName urlName=new URLName(server);
//PasswordAuthentication pa=new PasswordAuthentication(user,password);
Session sendMailSession=Session.getDefaultInstance(props,null);
sendMailSession.setDebug(true);
//sendMailSession.setPasswordAuthentication (urlName,pa);
//SMTPTransport transport=new SMTPTransport(sendMailSession,null);
Transport transport=sendMailSession.getTransport("smtp");
Message msg = new MimeMessage(sendMailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Java Mail Test");
msg.setHeader("X-Mailer", "msgsend");
msg.setSentDate(new Date());
//msg.setText ("Lalalala......");
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("Lalalala.......");
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource("c://msdos.sys");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
transport.connect (server,user,password);
//transport.connect ();
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
}
catch(Exception ex){
ex.printStackTrace();
}
}
|
http://www.csdn.net/expert/TopicView.asp?id=92577
|
props.put("mail.smtp.auth", "true");
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);