当前位置: 移动技术网 > IT编程>开发语言>Java > Javamail使用过程中常见问题解决方案

Javamail使用过程中常见问题解决方案

2020年08月06日  | 移动技术网IT编程  | 我要评论
今天在研究javamail发信的过程中,出现了一些小问题,现总结如下,以免后来者走些不必要的弯路,先把完整的能够正常运行的代码示例粘贴如下:发邮件源代码:import java.util.proper

今天在研究javamail发信的过程中,出现了一些小问题,现总结如下,以免后来者走些不必要的弯路,先把完整的能够正常运行的代码示例粘贴如下:

发邮件源代码:

import java.util.properties;
import javax.mail.*;
import javax.mail.internet.*;
 
public class mailexample {
 
 public static void main (string args[]) throws exception {
   
  string host = "smtp.163.com";  //发件人使用发邮件的电子信箱服务器
  string from = "你自己的电子信箱";  //发邮件的出发地(发件人的信箱)
  string to = "收件人信箱";  //发邮件的目的地(收件人信箱)
 
  // get system properties
  properties props = system.getproperties();
 
  // setup mail server
  props.put("mail.smtp.host", host);
 
  // get session
  props.put("mail.smtp.auth", "true"); //这样才能通过验证
 
  myauthenticator myauth = new myauthenticator("你自己的电子信箱", "你自己的信箱密码");
  session session = session.getdefaultinstance(props, myauth);
 
  //session.setdebug(true);
 
  // define message
  mimemessage message = new mimemessage(session);
   
 
  // set the from address
  message.setfrom(new internetaddress(from));
 
  // set the to address
  message.addrecipient(message.recipienttype.to,
   new internetaddress(to));
 
  // set the subject
  message.setsubject("测试程序!");
 
  // set the content
  message.settext("这是用java写的发送电子邮件的测试程序!");
  message.savechanges();
   transport.send(message);
 }
}

校验发信人权限的方法

import javax.mail.passwordauthentication;
 
class myauthenticator
   extends javax.mail.authenticator {
  private string struser;
  private string strpwd;
  public myauthenticator(string user, string password) {
   this.struser = user;
   this.strpwd = password;
  }
 
  protected passwordauthentication getpasswordauthentication() {
   return new passwordauthentication(struser, strpwd);
  }
 }

注意:上面的事例仅为使用163信箱时发送电子邮件的方法,因为使用的host为:smtp.163.com,如源代码中:string host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器,如果使用其它的电子邮件发送,就必须在其邮件服务器上查找相应的电子邮件服务器,例如搜狐就要使用smtp.sohu.com,具体情况具体对待,都可以从所使用的邮件服务器上获得的。如果没有使用host ,也就是说,没有进行props.put("mail.smtp.host", host);设置,那么就会抛javax.mail.messagingexception: could not connect to smtp host: localhost, port: 25;的异常。当然了,如果你没有正确配置,这个异常仍然会被抛出的。

有些邮件服务系统是不需要验证发件人的授权的,所以可以很简单的使用

session session = session.getdefaultinstance(props, null);
// 而不必使用
props.put("mail.smtp.auth", "true");
myauthenticator myauth = new myauthenticator("你自己的电子信箱", "你自己的信箱密码");
session session = session.getdefaultinstance(props, myauth);

就可以发送电子邮件了,这个多为一些企事业单位的内部电子信箱系统。

但是对于很多门户网站上的电邮系统,如:163,sohu,yahoo等等,如果仍然简单的这样使用就会抛

com.sun.mail.smtp.smtpsendfailedexception: 553 authentication is required,smtp8,wkjadxuaycafmnze8bwtia==.32705s2
at com.sun.mail.smtp.smtptransport.issuesendcommand(smtptransport.java:1388)
at com.sun.mail.smtp.smtptransport.mailfrom(smtptransport.java:959)
at com.sun.mail.smtp.smtptransport.sendmessage(smtptransport.java:583)
at javax.mail.transport.send0(transport.java:169)
at javax.mail.transport.send(transport.java:98)

这样的异常,要求你必须进行授权校验,它的目的就是阻止他人任意乱发邮件,也算是为了减少垃圾邮件的出现吧。这时候,我们就要使用

props.put("mail.smtp.auth", "true");
myauthenticator myauth = new myauthenticator("你自己的电子信箱", "你自己的信箱密码");
session session = session.getdefaultinstance(props, myauth);

这里还有一个特别注意的事情:在你使用session.getdefaultinstance时,一定要将 props.put("mail.smtp.auth", "true"); 置为true,它默认的是false,如果你没有做这一步,虽然你使用了session.getdefaultinstance(props, myauth);,你自己也确实 myauthenticator myauth = new myauthenticator("你自己的电子信箱", "你自己的信箱密码");但是它仍然会抛出

com.sun.mail.smtp.smtpsendfailedexception: 553 authentication is required,smtp8,wkjadxja2sbrm3zefv0gia==.40815s2

at com.sun.mail.smtp.smtptransport.issuesendcommand(smtptransport.java:1388)
at com.sun.mail.smtp.smtptransport.mailfrom(smtptransport.java:959)
at com.sun.mail.smtp.smtptransport.sendmessage(smtptransport.java:583)
at javax.mail.transport.send0(transport.java:169)
at javax.mail.transport.send(transport.java:98)

这样的异常。我就在这一步费了好长时间,后来才发现了这个问题,很是郁闷。不过还好,总算解决了。

其实上面的做法只是比较简单的一种,也有很多其它的写法,如:

properties props = system.getproperties();可以使用
properties props = new properties();来代替。

transport.send(message);可以使用下面的代码来代替
string username = "你的电子信箱用户名";
string password = "你的电子信箱密码";
message.savechanges(); // implicit with send()
transport transport = session.gettransport("smtp");
transport.connect("mail.htf.com.cn", username, password);
transport.sendmessage(message, message.getallrecipients());
transport.close();

这种方法在同时发送多封电子邮件时比较有用。

还有一些具体的相关概念,可以查看相关的官方文档,在我查询资料时,发现了一篇文章写得相当仔细,可以加以参考:另附上使用org.apache.commons.mail进行发电子邮件的示例:

import org.apache.commons.mail.simpleemail;
import org.apache.commons.mail.*;
 
public class testcommon {
  public testcommon() {
  }
 
  public static void main(string[] args) {
    simpleemail email = new simpleemail();
    email.sethostname("smtp.163.com");//设置使用发电子邮件的邮件服务器
    try {
      email.addto("收件人信箱");
      email.setauthentication("发件人信箱", "发件人信箱密码");
      email.setfrom("发件人信箱");
      email.setsubject("test apache.commons.mail message");
      email.setmsg("this is a simple test of commons-email");
      email.send();
    } catch (emailexception ex) {
      ex.printstacktrace();
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网