我如何使用java在没有身份验证的情况下发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22841632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:17:22  来源:igfitidea点击:

How i send email without authentication using java

javaemailauthenticationsmtp

提问by felipe muner

I would like to send email without authentication using java. Can someone help me?

我想使用java发送没有身份验证的电子邮件。有人能帮我吗?

With authentication, I do it as follows:

通过身份验证,我按如下方式进行:

    public void sendEmail() throws EmailException{
    SimpleEmail email = new SimpleEmail();
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(465);
    email.addTo("[email protected]", "XXXX");
    email.setFrom("[email protected]","XXXXX");
    email.setSubject("testando . . .");
    email.setMsg("testando 1");
    email.setSSL(true);
    email.setAuthentication("[email protected]", "XXXXX");
    email.send();
}

I forgot to say that i do not have a provider. i need an provider finally, i have emailFrom Subject and Message, and need send this email how?

我忘了说我没有提供者。我最终需要一个提供商,我有 emailFrom Subject 和 Message,需要如何发送这封电子邮件?

采纳答案by Paul Vargas

If it is only for testing purposes, you may try Papercut. While it's running, Papercut automatically picks up e-mail sent to the standard SMTP port (25) on any IP address. You just send mail from your application and switch to Papercut to review it.

如果仅用于测试目的,您可以尝试Papercut。在运行时,Papercut 会自动选取发送到任何 IP 地址上的标准 SMTP 端口 (25) 的电子邮件。您只需从您的应用程序发送邮件并切换到 Papercut 来查看它。

Papercut @ github: https://github.com/ChangemakerStudios/Papercut/releases

剪纸@github:https: //github.com/ChangemakerStudios/Papercut/releases

import java.util.Date;
import java.util.Properties;

import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.put("mail.smtp.host", "127.0.0.1");
        props.put("mail.smtp.port", "25");
        props.put("mail.debug", "true");
        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
        message.setSubject("Notification");
        message.setText("Successful!", "UTF-8"); // as "text/plain"
        message.setSentDate(new Date());
        Transport.send(message);
    }

}

回答by Adriano

Try this example:

试试这个例子:

// File Name SendEmail.java

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 = "[email protected]";

      // Sender's email ID needs to be mentioned
      String from = "[email protected]";

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

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

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);

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

         // Now set the actual message
         message.setText("This is actual message");

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

Via: tutorialspoint

通过:教程点

回答by sarbjeet singh

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

public class SimpleEmail {

    public static void main(String[] args) {
        System.out.println("SimpleEmail Start");
        String smtpHostServer = "smtp.gmail.com";
        String toEmail = "[email protected]";
        Properties props = System.getProperties();
        props.put("mail.smtp.host", smtpHostServer);
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.port", "25");
        Session session = Session.getDefaultInstance(props);
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
            msg.addHeader("format", "flowed");
            msg.addHeader("Content-Transfer-Encoding", "8bit");
            msg.setFrom(new InternetAddress("[email protected]", "NoReply-JD"));
            msg.setReplyTo(InternetAddress.parse("[email protected]", false));
            msg.setSubject("SimpleEmail Testing Subject", "UTF-8");
            msg.setText("SimpleEmail Testing Body", "UTF-8");
            msg.setSentDate(new Date());
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
            System.out.println("Message is ready");
            Transport.send(msg);
            System.out.println("EMail Sent Successfully!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}