【问题标题】:How do I send an SMTP Message from Java? [duplicate]如何从 Java 发送 SMTP 消息? [复制]
【发布时间】:2010-09-09 13:41:06
【问题描述】:

可能重复:
How do you send email from a Java app using Gmail?

如何从 Java 发送 SMTP 消息?

【问题讨论】:

标签: java smtp


【解决方案1】:

这是 Gmail smtp 的示例:

import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;

import javax.mail.*;

import javax.mail.internet.*;

import com.sun.mail.smtp.*;


public class Distribution {

    public static void main(String args[]) throws Exception {
        Properties props = System.getProperties();
        props.put("mail.smtps.host","smtp.gmail.com");
        props.put("mail.smtps.auth","true");
        Session session = Session.getInstance(props, null);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("mail@tovare.com"));;
        msg.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse("tov.are.jacobsen@iss.no", false));
        msg.setSubject("Heisann "+System.currentTimeMillis());
        msg.setText("Med vennlig hilsennTov Are Jacobsen");
        msg.setHeader("X-Mailer", "Tov Are's program");
        msg.setSentDate(new Date());
        SMTPTransport t =
            (SMTPTransport)session.getTransport("smtps");
        t.connect("smtp.gmail.com", "admin@tovare.com", "<insert password here>");
        t.sendMessage(msg, msg.getAllRecipients());
        System.out.println("Response: " + t.getLastServerResponse());
        t.close();
    }
}

现在,只有在您希望将项目依赖项保持在最低限度时才这样做,否则我强烈建议您使用来自 apache 的类

http://commons.apache.org/email/

问候

托夫是雅各布森

【讨论】:

  • sun mail 依赖让我在这里有点紧张——如果我使用的是非sun JRE 怎么办?
  • 是的,Java Mail API 需要协议实现,并且捆绑的参考实现被命名为 sun,但它是官方发行版的一部分。这些是纯 Java,应该适用于任何 Java 实现。这不是供应商的专有功能。
  • 可能你可以跳过com.sun的import语句:)
  • 这很好——因为我什至无法在 Windows 上的 Eclipse 中解析 com.sun.mail。这让我对 AIX 机器上的 IBM jre 不太乐观,我们之前在尝试使用 com.sun 包时遇到过问题。
  • @AmitW,我同意在我的示例中“抛出异常”不是处理异常的好方法。
【解决方案2】:

另一种方法是像这样使用阿司匹林 (https://github.com/masukomi/aspirin):

MailQue.queMail(MimeMessage message)

..按照上述方式构建你的 mimemessage 之后。

阿司匹林是一个 smtp '服务器',所以你不必配置它。但请注意,向广泛的收件人发送电子邮件并不像看起来那么简单,因为接收邮件服务器和客户端应用程序应用了许多不同的垃圾邮件过滤规则。

【讨论】:

  • 我希望他们能正确拼写“queue”。不会激发信心。
  • @brad 您提到的链接无效。当然,这是非常古老的答案。你能纠正一下吗?
  • 上面的固定链接
【解决方案3】:

请看这篇文章

How can I send an email by Java application using GMail, Yahoo, or Hotmail?

它特定于 gmail,但您可以替换您的 smtp 凭据。

【讨论】:

    【解决方案4】:

    请参阅 Java 实践中的以下教程。

    http://www.javapractices.com/topic/TopicAction.do?Id=144

    【讨论】:

      【解决方案5】:

      请参阅JavaMail API 和相关的 javadocs。

      【讨论】:

      • 欢迎举个简单的例子。
      【解决方案6】:
      import javax.mail.*;
      import javax.mail.internet.*;
      import java.util.*; 
      
      public void postMail(String recipients[], String subject,
          String message , String from) throws MessagingException {
      
          //Set the host smtp address
          Properties props = new Properties();
          props.put("mail.smtp.host", "smtp.jcom.net");
      
          // create some properties and get the default Session
          Session session = Session.getDefaultInstance(props, null);
          session.setDebug(false);
      
          // create a message
          Message msg = new MimeMessage(session);
      
          // set the from and to address
          InternetAddress addressFrom = new InternetAddress(from);
          msg.setFrom(addressFrom);
      
          InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
          for (int i = 0; i < recipients.length; i++) {
              addressTo[i] = new InternetAddress(recipients[i]);
          }
          msg.setRecipients(Message.RecipientType.TO, addressTo);
      
          // Optional : You can also set your custom headers in the Email if you Want
          msg.addHeader("MyHeaderName", "myHeaderValue");
      
          // Setting the Subject and Content Type
          msg.setSubject(subject);
          msg.setContent(message, "text/plain");
          Transport.send(msg);
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-06
        • 2019-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多