简体   繁体   English

如何使用JAVA代码发送电子邮件?

[英]How to send an Email using JAVA code?

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEMail
{
   public static void main(String [] args)
 {

  // Recipient's email ID needs to be mentioned.
  String to = "*********@gmail.com";

  // Sender's email ID needs to be mentioned
  String from = "********@gmail.com";

  // Assuming you are sending email from localhost
  String host = "465";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.port", host);
  properties.put("mail.smtp.user", "*****");
  properties.put("mail.smtp.host", "smtp.gmail.com");
  properties.put("mail.smtp.port", "587");
  properties.put("mail.smtp.starttls.enable","true");
  properties.put("mail.smtp.debug", "true");
  properties.put("mail.smtp.auth", "true");
  properties.put("mail.smtp.socketFactory.port", "587");
  properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  properties.put("mail.smtp.socketFactory.fallback", "false");
  properties.setProperty("mail.user", "*******");
  properties.setProperty("mail.password", "******");

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  try{
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
     message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
     message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));

     // Set Subject: header field
     message.setSubject("This is the Subject Line!");

     // Create the message part 
     BodyPart messageBodyPart = new MimeBodyPart();

     // Fill the message
     messageBodyPart.setText("This is message body");

     // Create a multipar message
     Multipart multipart = new MimeMultipart();

     // Set text message part
     multipart.addBodyPart(messageBodyPart);

     // Part two is attachment
     messageBodyPart = new MimeBodyPart();
     String filename = "file.txt";
     DataSource source = new FileDataSource(filename);
     messageBodyPart.setDataHandler(new DataHandler(source));
     messageBodyPart.setFileName(filename);
     multipart.addBodyPart(messageBodyPart);

     // Send the complete message parts
     message.setContent(multipart );

     // Send message
     Transport.send(message);
     System.out.println("Sent message successfully....");
  }catch (MessagingException mex) {
     mex.printStackTrace();
    }
   }
  }

I am getting the following error. 我收到以下错误。 Error: 错误:

javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException

After going through Suggestions from the same page.I have modified the code as shown above. 经过同一页面的建议后,我修改了上面的代码。 But, this time im getting the authentication error. 但是,这一次我收到身份验证错误。 Plz help me where iam wrong this time? 请帮我这次错在哪里?

  properties.setProperty("mail.smtp.host", host);

Here, host is not the machine where you are sending the email from.(As written in comments in code). 在这里,主机不是您从中发送电子邮件的机器。(如代码注释中所写)。 It is the host where your e-mail server is. 它是您的电子邮件服务器所在的主机。

For gmail - it is smtp.gmail.com on port 465. 对于smtp.gmail.com端口465是smtp.gmail.com

You have to configure it according to your smtp server. 您必须根据您的smtp服务器进行配置。

You need to more properties, I can see your code and see that there is some properties are missing like 您需要更多属性,我可以看到您的代码,并看到缺少​​一些属性,例如

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");   

You also need to set Authenticator and some other things, Please refer below Example: Send Mail using JAVA with Gmail Account 您还需要设置Authenticator和其他一些设置,请参考以下示例: 使用JAVA和Gmail帐户发送邮件

you can find the code here to send email using Java 您可以在此处找到使用Java发送电子邮件的代码

http://javainfinite.com/java/send-email-using-gmail-in-java/ http://javainfinite.com/java/send-email-using-gmail-in-java/

个人建议使用原始的SMTP协议发送邮件,只是为了更好地了解它的工作原理,但为了解决他的问题,请检查防火墙中的端口25是否不应该打开,启动新规则

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

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