如何从 Java 发送 SMTP 消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/73580/
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
How do I send an SMTP Message from Java?
提问by Allain Lalonde
Possible Duplicate:
How do you send email from a Java app using Gmail?
How do I send an SMTP Message from Java?
如何从 Java 发送 SMTP 消息?
采纳答案by tovare
Here's an example for Gmail smtp:
以下是 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("[email protected]"));;
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]", 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", "[email protected]", "<insert password here>");
t.sendMessage(msg, msg.getAllRecipients());
System.out.println("Response: " + t.getLastServerResponse());
t.close();
}
}
Now, do it this way only if you would like to keep your project dependencies to a minimum, otherwise i can warmly recommend using classes from apache
现在,仅当您希望将项目依赖项保持在最低限度时才这样做,否则我可以热烈推荐使用来自 apache 的类
http://commons.apache.org/email/
http://commons.apache.org/email/
Regards
问候
Tov Are Jacobsen
托夫·雅各布森
回答by Mason
See the JavaMail APIand associated javadocs.
请参阅JavaMail API和相关的 javadoc。
回答by Ryan Lanciaux
Please see this post
请看这个帖子
How can I send an email by Java application using GMail, Yahoo, or Hotmail?
如何使用 GMail、Yahoo 或 Hotmail 通过 Java 应用程序发送电子邮件?
It is specific to gmail but you can substitute your smtp credentials.
它特定于 gmail,但您可以替换您的 smtp 凭据。
回答by Jorge Ferreira
See the following tutorial at Java Practices.
请参阅 Java 实践中的以下教程。
回答by Brad at Kademi
Another way is to use aspirin (https://github.com/masukomi/aspirin) like this:
另一种方法是像这样使用阿司匹林(https://github.com/masukomi/aspirin):
MailQue.queMail(MimeMessage message)
..after having constructed your mimemessage as above.
..在按照上面构建你的 mimemessage 之后。
Aspirin isan smtp 'server' so you don't have to configure it. But note that sending email to a broad set of recipients isnt as simple as it appears because of the many different spam filtering rules receiving mail servers and client applications apply.
阿司匹林是一个 smtp“服务器”,因此您不必对其进行配置。但请注意,向广泛的收件人发送电子邮件并不像看起来那么简单,因为接收邮件服务器和客户端应用程序应用了许多不同的垃圾邮件过滤规则。
回答by user527619
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);
}