简体   繁体   English

无法通过java mail API发送电子邮件?

[英]Can't able send email via java mail API?

I want to send mail from my java(JSP) project that is basically a Web based project to user. 我想从我的java(JSP)项目发送邮件,该项目基本上是一个基于Web的项目给用户。 But i'm getting an error.give me solution of this. 但是我得到了一个错误。我解决了这个问题。

I tried a lot of solution that is provided on net but nothing will work out. 我尝试了很多在网上提供的解决方案,但没有什么能解决的。 I changed port no 465 to 587 but my error is still there. 我将465号端口更改为587但我的错误仍然存​​在。

import java.io.UnsupportedEncodingException;  
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
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;  

public class MailUtil {    
    private String from1 = "fromemail@gmail.com";  
    private String  password= "*****";  
    private String FROM_NAME = "abc";  

    public boolean sendMail(String[] recipients, String[] bccRecipients, String subject, String message) {  
        try {  
            Properties props = new Properties(); 
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");  
            props.put("mail.smtp.auth", "true");
           // props.put("mail.smtp.socketFactory.port", "465"); 
            props.put("mail.smtp.port", "587");
       props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            props.put("mail.debug", "false");  
            props.put("mail.smtp.ssl.enable", "true");  

            Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from1, password);
                }
            });  
            Message msg = new MimeMessage(session);  

            InternetAddress from = new InternetAddress(from, name);  
            msg.setFrom(from);  

            InternetAddress[] toAddresses = new InternetAddress[recipients.length];  
            for (int i = 0; i < recipients.length; i++) {  
                toAddresses[i] = new InternetAddress(recipients[i]);  
            }  
            msg.setRecipients(Message.RecipientType.TO, toAddresses);  


            InternetAddress[] bccAddresses = new InternetAddress[bccRecipients.length];  
            for (int j = 0; j < bccRecipients.length; j++) {  
                bccAddresses[j] = new InternetAddress(bccRecipients[j]);  
            }  
            msg.setRecipients(Message.RecipientType.BCC, bccAddresses);  

            msg.setSubject(subject);  
            msg.setContent(message, "text/plain");  
            Transport.send(msg);  
            return true;  
        } catch (UnsupportedEncodingException ex) {  
            Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);  
            return false;  

        } catch (MessagingException ex) {  
            Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);  
            return false;  
        }  
    }  
}

SEVERE: null 严重:空

javax.mail.SendFailedException: Sending failed; javax.mail.SendFailedException:发送失败; nested exception is: class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. 嵌套异常是:class javax.mail.MessagingException:530 5.7.0必须首先发出STARTTLS命令。 d129sm8507654pfa.142 - gsmtp d129sm8507654pfa.142 - gsmtp

at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)

First, fix all these common JavaMail mistakes . 首先,修复所有这些常见的JavaMail错误

If that doesn't fix the problem, post the JavaMail debug output . 如果这不能解决问题,请发布JavaMail调试输出

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

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