如何配置环境以使用JavaMail?
我需要用JavaMail发送简单的html消息。当我试图在互联网上找到一些很好的例子和解释时,下一个例子让我越来越生气 所有这些愚蠢的例子都包含复制和粘贴的Java代码,这些代码只在注释上有所不同,还有一个很好的免责声明,即首先应该配置smtp和pop3服务器 我知道没有人想为一些具体的产品做广告,但配置服务器是最难的部分。那么,有谁能给我一些关于配置具体服务器(例如Kerio或任何其他服务器)的真正有用的信息(没有java代码)吗 我现在看到的是下一个例外:如何配置环境以使用JavaMail?,java,jakarta-mail,mail-server,Java,Jakarta Mail,Mail Server,我需要用JavaMail发送简单的html消息。当我试图在互联网上找到一些很好的例子和解释时,下一个例子让我越来越生气 所有这些愚蠢的例子都包含复制和粘贴的Java代码,这些代码只在注释上有所不同,还有一个很好的免责声明,即首先应该配置smtp和pop3服务器 我知道没有人想为一些具体的产品做广告,但配置服务器是最难的部分。那么,有谁能给我一些关于配置具体服务器(例如Kerio或任何其他服务器)的真正有用的信息(没有java代码)吗 我现在看到的是下一个例外: 250 2.0.0 Reset s
250 2.0.0 Reset state
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Relaying to <mymail@mycompany.com> denied (authentication required)
如果您正在寻找配置SMTP服务器的教程,那么不应该寻找JavaMail。只需在您选择的服务器上查找教程(,例如,…或,,)或继续询问即可。任何兼容SMTP的服务器都可以很好地使用JavaMail 或者,您甚至可以使用任何“标准”邮件提供商的基础设施。例如,我使用一个帐户和Google的SMTP基础设施从Java应用程序发送邮件。如果您不想为了简单地测试驱动JavaMail而设置自己的SMTP服务器,那么这是一个很好的起点 作为最后一个选项,您甚至可以查找域并将邮件直接发送到收件人的SMTP服务器。有一些常见的难题需要解决 最后一点,你必须研究如何避免你的邮件被过滤成垃圾邮件——这本身就是一个巨大的话题。在这里,依赖标准提供者将有助于解决托管自己的服务器时可能遇到的一些问题 顺便说一句:关于您发布的错误消息:SMTP服务器拒绝转发消息。这是如果您的SMTP服务器(认为它)正在example.com上运行,并且您以bob@example.net到alice@example.org,您要求SMTP服务器充当中继。这是几年前的常见做法,直到-你猜到了-被垃圾邮件发送者滥用。从那时起,邮政局长被鼓励拒绝转寄邮件。您有两种选择:在发送邮件之前进行身份验证,或仅发送到服务器上托管的帐户(例如,在example.com上,例如。alice@example.com). 编辑: 下面是一些代码,让您开始进行身份验证(适用于Gmail帐户,但也适用于您自己的服务器)
我能看出你的部分问题。错误消息中对此进行了充分解释 您将邮件发送到的SMTP服务器(即您在JavaMail配置中配置的地址之一)拒绝将邮件转发到
mymail@company.com
。SMTP服务器中似乎存在配置问题。正如Sfussenger所指出的,它与javamail无关
因此,您并不是同时在所有方面进行调试,最好尝试从一个已知的工作SMTP客户端寻址SMTP服务器。例如,雷鸟会做得很好。如果您可以通过Thunderbird发送邮件,那么JavaMail应该不会有什么问题
更新: 谷歌SMTP服务器的正确地址是:
SMTP.gmail.com
。这是您在JavaMail中配置的服务器吗?您可以向我们显示匹配的错误消息吗?这应该可以:
import java.text.MessageFormat;
import java.util.List;
import java.util.Properties;
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 Emailer {
public static void main(String[] args) {
String hostname = args[0];
final String userName = args[1];
final String passWord = args[2];
String toEmail = args[3];
String fromEmail = args[4];
String subject = args[5];
String body = "";
// add rest of args as one body text for convenience
for (int i = 6; i < args.length; i++) {
body += args[i] + " ";
}
Properties props = System.getProperties();
props.put("mail.smtp.host", hostname);
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, passWord);
}
});
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
} catch (MessagingException e) {
System.out.println("Cannot send email " + e);
}
}
}
导入java.text.MessageFormat;
导入java.util.List;
导入java.util.Properties;
导入javax.mail.Authenticator;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.mimessage;
公共类电子邮件发送程序{
公共静态void main(字符串[]args){
字符串hostname=args[0];
最终字符串userName=args[1];
最终字符串密码=args[2];
字符串toEmail=args[3];
字符串fromEmail=args[4];
字符串主题=args[5];
字符串体=”;
//为方便起见,将其余参数添加为一个正文文本
对于(int i=6;i
对于javax.mail依赖项,您需要将JavaMail.jar放在类路径上。
我不确定谷歌是否允许你随心所欲地发送电子邮件。试试另一个电子邮件提供商,比如你的ISP,怎么样?一个结合上述答案的工作示例,使用激活-1.1.jar和邮件-1.4.1.jar,SMTP主机是Gmail
user@gmail.com
和用户
第行返回新密码身份验证(“user@gmail.com“,”用户“代码>
myRecipientAddress@gmail.com
通过您希望接收电子邮件的电子邮件地址
package com.test.sendEmail;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class sendEmailTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
sendEmailTest emailer = new sendEmailTest();
//the domains of these email addresses should be valid,
//or the example will fail:
emailer.sendEmail();
}
/**
* Send a single email.
*/
public void sendEmail(){
Session mailSession = createSmtpSession();
mailSession.setDebug (true);
try {
Transport transport = mailSession.getTransport ();
MimeMessage message = new MimeMessage (mailSession);
message.setSubject ("HTML mail with images");
message.setFrom (new InternetAddress ("myJavaEmailSender@gmail.com"));
message.setContent ("<h1>Hello world</h1>", "text/html");
message.addRecipient (Message.RecipientType.TO, new InternetAddress ("myRecipientAddress@gmail.com"));
transport.connect ();
transport.sendMessage (message, message.getRecipients (Message.RecipientType.TO));
}
catch (MessagingException e) {
System.err.println("Cannot Send email");
e.printStackTrace();
}
}
private Session createSmtpSession() {
final Properties props = new Properties();
props.setProperty ("mail.host", "smtp.gmail.com");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", "" + 587);
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty ("mail.transport.protocol", "smtp");
// props.setProperty("mail.debug", "true");
return Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user@gmail.com", "user_pw");
}
});
}
}
package com.test.sendmail;
导入java.util.Properties;
导入javax.mail.*;
导入javax.mail.internet.*;
公共类发送电子邮件测试{
/**
*@param args
*/
公共静态void main(字符串[]args){
private Session createSmtpSession() {
final Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", "" + 587);
props.setProperty("mail.smtp.starttls.enable", "true");
// props.setProperty("mail.debug", "true");
return Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("john.doe@gmail.com", "mypassword");
}
});
}
import java.text.MessageFormat;
import java.util.List;
import java.util.Properties;
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 Emailer {
public static void main(String[] args) {
String hostname = args[0];
final String userName = args[1];
final String passWord = args[2];
String toEmail = args[3];
String fromEmail = args[4];
String subject = args[5];
String body = "";
// add rest of args as one body text for convenience
for (int i = 6; i < args.length; i++) {
body += args[i] + " ";
}
Properties props = System.getProperties();
props.put("mail.smtp.host", hostname);
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, passWord);
}
});
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
} catch (MessagingException e) {
System.out.println("Cannot send email " + e);
}
}
}
package com.test.sendEmail;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class sendEmailTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
sendEmailTest emailer = new sendEmailTest();
//the domains of these email addresses should be valid,
//or the example will fail:
emailer.sendEmail();
}
/**
* Send a single email.
*/
public void sendEmail(){
Session mailSession = createSmtpSession();
mailSession.setDebug (true);
try {
Transport transport = mailSession.getTransport ();
MimeMessage message = new MimeMessage (mailSession);
message.setSubject ("HTML mail with images");
message.setFrom (new InternetAddress ("myJavaEmailSender@gmail.com"));
message.setContent ("<h1>Hello world</h1>", "text/html");
message.addRecipient (Message.RecipientType.TO, new InternetAddress ("myRecipientAddress@gmail.com"));
transport.connect ();
transport.sendMessage (message, message.getRecipients (Message.RecipientType.TO));
}
catch (MessagingException e) {
System.err.println("Cannot Send email");
e.printStackTrace();
}
}
private Session createSmtpSession() {
final Properties props = new Properties();
props.setProperty ("mail.host", "smtp.gmail.com");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", "" + 587);
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty ("mail.transport.protocol", "smtp");
// props.setProperty("mail.debug", "true");
return Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user@gmail.com", "user_pw");
}
});
}
}