Javamail - 从本地主机向外部帐户发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24202926/
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
Javamail - sending mail from localhost to external accounts
提问by Karthick Radhakrishnan
Need to send email from localhost to external accounts like gmail and yahoo. Right now i have a program which can send and recieve mails from my local domain by local email server up and running eg ([email protected] <-> [email protected]). But problem is when i try to send from local domain to gmail or yahoo account i'm Unable to do it eg([email protected] -> [email protected]). Need help on this
需要从本地主机向 gmail 和 yahoo 等外部帐户发送电子邮件。现在我有一个程序可以通过本地电子邮件服务器从我的本地域发送和接收邮件,例如([email protected] <-> [email protected])。但问题是当我尝试从本地域发送到 gmail 或 yahoo 帐户时,我无法做到,例如([email protected] -> [email protected])。需要这方面的帮助
PS. I'm Using Hmailserver for emailserver
附注。我正在使用 Hmailserver 作为电子邮件服务器
public class JMailer {
private static String HOSTNAME = "localhost";
private static String USERNAME = "admin";
private static String PASSWORD = "Mylocaldomainpassword";
public static void main(String[] args) {
try {
String to = "[email protected]";
String from = "[email protected]";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host",HOSTNAME);
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("My Subject!");
message.setText("Here Goes My Message");
Transport.send(message);
System.out.println("Message Sending Completed");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
and my error from Hmailserver log is below
我的 Hmailserver 日志错误如下
"SMTPC" 4508 0 "2014-06-13 15:18:01.022" "TCP" "SMTPDeliverer - Message 13 - Connection failed: Host name: 74.125.25.27, message: No connection could be made because the target machine actively refused it"
"SMTPC" 4508 0 "2014-06-13 15:18:01.022" "TCP" "SMTPDeliverer - 消息 13 - 连接失败:主机名:74.125.25.27,消息:无法建立连接,因为目标机器主动拒绝它”
did i miss anything here?why remote machine's connection is refused ? and i dont want to use gmail's SMTP server to send message.all i need is i want my own smtp sever running to send and recieve
我错过了什么吗?为什么远程机器的连接被拒绝?我不想使用 gmail 的 SMTP 服务器来发送消息。我需要的是我想要我自己的 smtp 服务器运行来发送和接收
采纳答案by Karthick Radhakrishnan
Finally i'm able to crack this, I tested in 2 email servers which can get our job done,Apache james and hmailserver. Hmailserver is pretty easy run and configure because it has gui to do that.
最后我能够破解这个问题,我在 2 个电子邮件服务器上进行了测试,它们可以完成我们的工作,Apache james 和 hmailserver。Hmailserver 很容易运行和配置,因为它有 gui 来做到这一点。
HmailServer 5.4.2
HmailServer 5.4.2
1. Configure as per the documentation
2. Do not use localhost and make sure you change it in C:\Windows\System32\drivers\etc\hosts from "127.0.0.1 localhost" -> "127.0.0.1 example.com"
3. In add domain of hmailserver give "example.com"
4. In Domain created add accounts [email protected]
5. under setting->protocold->smtp->delivery of email add "example.com"
Now run the below program we are good to go.
现在运行下面的程序,我们很高兴。
Apache James apache-james-3.0-beta4
Apache James apache-james-3.0-beta4
Mostly same as above but this do not have any GUI to configure,this is light weight command line email server which also runs on Linux.
大部分与上面相同,但没有任何 GUI 可配置,这是一个轻量级的命令行电子邮件服务器,它也可以在 Linux 上运行。
apply same procedure but it has specific command line to create domain and accounts, before that you need to log into admin account to create users.
应用相同的程序,但它有特定的命令行来创建域和帐户,在此之前您需要登录管理员帐户来创建用户。
the hurdle you will face using Apache james is it runs well with 32 bit, but it will have server starting issue for 64 bit because the "Wrapper.exe " it uses from tanuki. where thy dont support 64 bit version of wrapper.exe so i had to but trial license and add 64 bit wrapper.exe and modify james.bat.
使用 Apache james 将面临的障碍是它在 32 位上运行良好,但它会在 64 位上出现服务器启动问题,因为它使用来自tanuki的“Wrapper.exe” 。你不支持 64 位版本的 wrapper.exe 所以我不得不试用许可证并添加 64 位 wrapper.exe 并修改 james.bat。
Other than that it works fine.
除此之外它工作正常。
package com.javabp.jmailer;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JMailer {
public static void main(String[] args)
{
/***** CHANGE THESE FOUR VARIABLE VALUES TO REFLECT YOUR ENVIRONMENT ******/
String user = "user"; // Newly created user on JAMES Server
String password = "password"; // user password
String fromAddress = "[email protected]"; // newlycreateduser@localhost
String toAddress = "[email protected]";
// Create a mail session
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "example.com");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.username", user);
properties.put("mail.smtp.password", password);
Session session = Session.getDefaultInstance(properties, null);
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
message.setSubject("Email From my Own Server");
message.setText("Test Mail sent from My Apache James Server!!");
Transport.send(message);
System.out.println("Email sent successfully");
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
}
the above code works for both Hmailserver and Apache James.
上面的代码适用于 Hmailserver 和 Apache James。
Pointer for emails
电子邮件指针
* if your sending to external accounts be sure you see you mail at spam folders unless you have registered domain and hosted.
* once you send a mail to those server there is been chance your IP or domain will be blocked especially gmail. so it is better to have dynamic IP so you can restart your internet connection to send a mail again and also make sure you change your domain before sending even you changed your IP.
回答by Pavan Patidar
Try this. Working perfectly! put your Gmail ID at [email protected]
, and your Gmail password at password.
尝试这个。完美运行!将您的 Gmail ID 放在[email protected]
,将您的 Gmail 密码放在 password。
import com.sun.mail.smtp.SMTPMessage;
import java.util.Properties;
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 SendmailSSl {
public static void main(String[] args) {
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", "805");
Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","Password");
}
});
try {
SMTPMessage message = new SMTPMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse( "[email protected]" ));
message.setSubject("Testing Subject");
message.setText("This is Test mail");
message.setContent("This Is my First Mail Through Java");
message.setNotifyOptions(SMTPMessage.NOTIFY_SUCCESS);
int returnOption = message.getReturnOption();
System.out.println(returnOption);
Transport.send(message);
System.out.println("sent");
}
catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}