【问题标题】:Java Mail Error In Sending Mails发送邮件时出现 Java 邮件错误
【发布时间】:2019-08-16 18:13:51
【问题描述】:

我正在使用一个程序来发送电子邮件。当我使用其他邮件服务器时,该代码有效。但我需要使用我公司的电子邮件帐户发送电子邮件。邮箱账号由gmailxxxx@companyname.com提供。当我将mail host 更改为`stmp.gmail.com 时,遇到以下错误:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command           first. st6sm11092256pbc.58

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1515)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1054)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:634)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:118)
at Mail.sendMail(Mail.java:48)
at Test.main(Test.java:6)

代码如下

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class Email_Autherticator extends Authenticator {
    String username = "xxxx@gmail";
    String password = "xxxxx";

    public Email_Autherticator() {
        super();
    }
    public Email_Autherticator(String user,String pwd){
        super();
        username = user;
        password = pwd;
    }

    public PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(username,password);
    }
} 


import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
    private String host = "smtp.gmail.com";
    private String mail_head_name = "this is head of this mail";
    private String mail_head_value = "this is head of this mail";
    private String mail_to = "xxxx@gmail.com";
    private String mail_from = "xxxx@Comanyname.com";//using gmail server
    private String mail_subject = "this is the subject of this test mail";
    private String mail_body = "this is mail_body of this test mail";
    private String personalName = "xxx";

    public void sendMail() throws SendFailedException{
        try {
            Properties props = new Properties();
            Authenticator auth = new Email_Autherticator();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            System.out.println(props);
            Session session = Session.getDefaultInstance(props,auth);

            MimeMessage message = new MimeMessage(session);
            message.setContent("Hello","text/plain");
            message.setSubject(mail_subject);
            message.setText(mail_body);
            message.setHeader(mail_head_name, mail_head_value);
            message.setSentDate(new Date());
            Address address = new InternetAddress(mail_from,personalName);
            message.setFrom(address);
            Address toaddress = new InternetAddress(mail_to);
            message.addRecipient(Message.RecipientType.TO,toaddress);
            System.out.println(message);
            Transport.send(message);
            System.out.println("Send Mail Ok!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        //return flag;
    }
}

【问题讨论】:

标签: java jakarta-mail


【解决方案1】:

您几乎可以肯定只需要重新编写代码以添加在JavaMail API - Sending email via Gmail SMTP example 示例中定义的属性。

您可以将props 设置为此:

Properties properties = new Properties();

properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.host", "smtp.gmail.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.user", gmailUsername);
properties.setProperty("mail.smtp.password", gmailPassword);

因为这似乎是为了工作——如果可以的话——我建议使用 Spring。它使它更清洁,更易于使用。我最近刚刚使用 Spring 和 Gmail SMTP 做了类似于 this 的操作。

【讨论】:

    【解决方案2】:

    您需要在您的 java 代码中定义以下属性。

    在包下导入

    import java.util.Properties;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    
    

    并将以下内容添加到您的代码中:

    Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "587");
    

    使用 javax.mail.authenticator 向 gmail 服务器进行身份验证

    
    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
                }
            });
    
    

    试试这个应该对你有用。

    如果仍有问题,请参阅此错误文档: https://pepipost.com/tutorials/common-javamail-smtp-errors/

    用于使用 javaMail API 发送 SMTP 电子邮件

    【讨论】:

    • 您应该将重要的部分添加到您的答案中。展示如何定义需求。
    猜你喜欢
    • 2014-01-13
    • 2013-01-07
    • 2014-09-26
    • 2012-02-15
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多