繁体   English   中英

通过smtp和gettin发送邮件错误?

[英]Sending mail through smtp and gettin error?

这是我的代码。 我收到以下异常。

final String username = "mymail@gmail.com";
final String password = "mypass";

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

Session session = Session.getInstance(props, new javax.mail.Authenticator() {
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
        return new javax.mail.PasswordAuthentication(username, password);
    }
});
try {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("fazeen.ahmad93@gmail.com"));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("fazeenahmad1993@gmail.com"));
    message.setSubject("Testing subject");

    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText("test body");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    messageBodyPart = new MimeBodyPart();
    message.setContent(multipart);

    Transport.send(message);

我收到的异常:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. pl7sm1333988wic.4 - gsmtp
  at Test.main(Test.java:200)
       Caused by: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. pl7sm1333988wic.4 - gsmtp
  at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)
  at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716)
  at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388)
  at javax.mail.Transport.send0(Transport.java:169)
  at javax.mail.Transport.send(Transport.java:98)
  at Test.main(Test.java:195)

我认为您缺少将以下保险丝添加到属性中的功能。

mail.smtp.starttls.required=true

使用此运行代码发送邮件

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author xyz
 */
public class MailSend {
    public static void main (String [] args){
         String to="xyz@gmail.com";//change accordingly

//Get the session object
  Properties props = new Properties();
  props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", "465");

  Session session = Session.getDefaultInstance(props,
   new javax.mail.Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication("email_address","password");//change accordingly
   }
  });

//compose message
  try {
   MimeMessage message = new MimeMessage(session);
   message.setFrom(new InternetAddress("email_address"));//change accordingly
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
   message.setSubject("Welcome!!");

    message.setText("Hiii buddy ");



   Transport.send(message);

   System.out.println("message sent successfully");

  } catch (MessagingException e) {throw new RuntimeException(e);}

 }
    }

如果您的计算机上没有smtp服务器,请下载该服务器并在下载后运行。

尝试这个

我当时使用的是旧版本的mail.jar,请使用此jar mail-1.4.7.jar进行身份验证。 要打开帐户访问权限,请执行以下操作: https : //www.google.com/settings/security/lesssecureapps (打开)如果使用谷歌SMTP的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM