从本地机器邮件服务器向gmail或其他java邮件服务器发送邮件
我有以下代码从本地机器邮件服务器向gmail或其他java邮件服务器发送邮件,java,email,gmail,jakarta-mail,mercury-mta,Java,Email,Gmail,Jakarta Mail,Mercury Mta,我有以下代码 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 =
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 = "*****@gmail.com";
// Sender's email ID needs to be mentioned
String from = "test1@localhost";
// 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();
}
}
}
Mercury服务器日志
服务器调试输出
我已经在本地机器上安装了Mercury邮件服务器,现在我正在尝试将邮件从本地机器邮件服务器发送到gmail用户。已成功向我发送消息。。。。输出当我执行上述代码时。但是,我在gmail帐户中没有收到任何邮件。jWeaver,我遇到了一个类似的问题,并以一种不太清楚解释原因的方式解决了问题,但很有效 根据以下内容将属性模式更改为:
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.debug", "true");
您查看过垃圾邮件箱吗?一分钟后,我将分析您的代码。@请解释我的问题出了什么问题?验证您的邮件服务器是否正在接收来自您的应用程序的邮件。为此,您可能需要在服务器上启用日志记录。如果没有收到邮件,您需要查看应用程序的设置方式,以便向其发送邮件。如果收到邮件,问题在于您的邮件服务器配置或路由,并且更改此代码的主题不在:addRecipient to setRecipients and test。我已经包含了您的代码,看起来很好,我没有收到任何错误。但是,仍然没有收到邮件:伙计,我建议你在你的机器上安装另一个邮件服务器,进行测试。可能有问题。检查门是否打开。在代码中配置它:props.putmail.smtp.port,465;我使用465端口,但你使用你想要的端口。检查你的防火墙。
DEBUG: JavaMail version 1.5.0
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
220 localhost ESMTP server ready.
DEBUG SMTP: connected to host "localhost", port: 25
EHLO RAVI-PC
250-localhost Hello RAVI-PC; ESMTPs are:
250-TIME
250-SIZE 0
250 HELP
DEBUG SMTP: Found extension "TIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "0"
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<test1@localhost>
250 Sender OK - send RCPTs.
RCPT TO:<mygmailaccount@gmail.com>
250 Recipient OK - send RCPT or DATA.
DEBUG SMTP: Verified Addresses
DEBUG SMTP: mygmailaccount@gmail.com
DATA
354 OK, send data, end with CRLF.CRLF
From: test1@localhost
To: mygamilaccount@gmail.com
Message-ID: <22999979.0.1396766922465.JavaMail.RAVI@RAVI-PC>
Subject: This is the Subject Line!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
This is actual message
.
250 Data received OK.
QUIT
221 localhost Service closing channel.
Sent message successfully....
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.debug", "true");