[英]Unable to send gmail mail
I am trying to write a java code to send an email via Gmail , I was able to get the correct configuration and code, and have tested my code at home, (email was sent successfully from home), problems appear when I run the same code at work, I was able to figure out the problem is on the network, so I asked my NW admin to open ports 465 and 587 for me and start facing a new issue that I am unable to establish a connection with smtp.gmail.com on both ports , I installed telnet, and run the following command 我正在尝试编写Java代码以通过Gmail发送电子邮件,我能够获得正确的配置和代码,并且已经在家中测试了我的代码((电子邮件已成功从家里发送)),当我运行相同的代码时会出现问题代码在工作时,我能够确定问题出在网络上,所以我请西北管理员为我打开端口465和587,并开始面临一个新问题,即我无法与smtp.gmail建立连接。两个端口上的com,我安装了telnet,并运行以下命令
telnet smtp.gmail.com 465
and the telnet connection is acting in such a weird way (command shows only underscore (_) and nothing appears, see attached image), note that when I run the same command on any other machine at work it runs fine. 并且telnet连接的行为很奇怪(命令仅显示下划线(_),什么都没有出现,请参见附件图像),请注意,当我在工作的任何其他计算机上运行同一命令时,它运行良好。
I turned off windows firewall, anti-virus . 我关闭了Windows防火墙,防病毒。 and still unable to determine what I need to do to get it works since the issue is only my PC.
仍然无法确定我需要做什么才能使其正常工作,因为问题仅是我的PC。 here is my code:
这是我的代码:
package mail;
private SendMail() {
}
public static void main(String[] args) throws AddressException, MessagingException {
try {
send("mySenderMail@gmail.com", "myPassword", "myRecieverMail@gmail.com", "test java code mail", "this is a text message string");
System.out.println("Sent Sucessfully");
}
catch (AddressException exc) {
System.out.println("exception"+ exc.getMessage());
System.out.println("cause:"+ exc.getCause());
}
catch(MessagingException exc ){
System.out.println("exception"+ exc.getMessage());
System.out.println("cause:"+ exc.getCause());
}
catch(Exception exc){
System.out.println("exception"+ exc.getMessage());
System.out.println("cause:"+ exc.getCause());
}
}
public static void send(final String username, final String password, String recipientEmail, String title,
String message) throws AddressException, MessagingException,ArrayIndexOutOfBoundsException , IOException {
SendMail.send(username, password, recipientEmail, "", title, message);
}
public static void send(final String username, final String password, String recipientEmail, String ccEmail,
String title, String message) throws AddressException, MessagingException , ArrayIndexOutOfBoundsException, IOException {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// Get a Properties object
String emailHost = username.split("@")[1].toLowerCase() + ".";
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream stream = loader.getResourceAsStream("mail/mailConfig.properties");
prop.load(stream);
String smtpHost = prop.getProperty(emailHost + "smtpHost");
String socketFactoryClass = prop.getProperty(emailHost + "socketFactoryClass");
String socketFactoryFallback = prop.getProperty(emailHost + "socketFactoryFallback");
String smtpPort = prop.getProperty(emailHost + "smtpPort");
String socketFactoryPort = prop.getProperty(emailHost + "socketFactoryPort");
String auth = prop.getProperty(emailHost + "auth");
String starttlsEnable = prop.getProperty(emailHost + "starttlsEnable");
String debug = prop.getProperty(emailHost + "debug");
String quitwait = prop.getProperty(emailHost + "quitwait");
Properties props = System.getProperties();
if (smtpHost != null && !smtpHost.isEmpty())
props.setProperty("mail.smtp.host", smtpHost);
if (socketFactoryClass != null && !socketFactoryClass.isEmpty())
props.setProperty("mail.smtp.socketFactory.class", socketFactoryClass);
if (socketFactoryFallback != null && !socketFactoryFallback.isEmpty())
props.setProperty("mail.smtp.socketFactory.fallback", socketFactoryFallback);
if (smtpPort != null && !smtpPort.isEmpty())
props.setProperty("mail.smtp.port", smtpPort);
if (socketFactoryPort != null && !socketFactoryPort.isEmpty())
props.setProperty("mail.smtp.socketFactory.port", socketFactoryPort);
if (auth != null && !auth.isEmpty())
props.setProperty("mail.smtp.auth", auth);
if (starttlsEnable != null && !starttlsEnable.isEmpty())
props.setProperty("mail.smtp.starttls.enable", starttlsEnable);
if (debug != null && !debug.isEmpty())
props.setProperty("mail.smtp.debug", debug);
if (quitwait != null && !quitwait.isEmpty())
props.put("mail.smtps.quitwait", quitwait);
Session session = Session.getInstance(props, null);
// -- Create a new message --
final MimeMessage msg = new MimeMessage(session);
session.setDebug(true);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
t.connect(smtpHost, username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
and here is my configuration file contents : 这是我的配置文件内容:
## ----------------- Gmail -----------------##
gmail.com.smtpHost = smtp.gmail.com
gmail.com.socketFactoryClass = javax.net.ssl.SSLSocketFactory
gmail.com.socketFactoryFallback = false
gmail.com.smtpPort = 465
gmail.com.socketFactoryPort = 465
gmail.com.auth = true
gmail.com.starttlsEnable = true
gmail.com.debug = true
gmail.com.quitwait = false
btw: I have got the following exception when I run my code 顺便说一句:运行代码时出现以下异常
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at mail.SendMail.send(SendMail.java:213)
at mail.SendMail.send(SendMail.java:111)
at mail.SendMail.main(SendMail.java:26)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.security.ssl.InputRecord.readFully(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:548)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:207)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
I really don't know what I have to do next, please help. 我真的不知道接下来要做什么,请帮忙。
I've not tested this yet since i'm in an enclosed environment but I suppose what you're trying to do here is keeping the connection as TLS while using port which is used for SSL. 由于我处于封闭环境中,因此我尚未进行测试,但我想您要在此处进行的操作是在使用用于SSL的端口时将连接保持为TLS。
As per the gmail specification, they use port-465 for SSL while port-587 for TLS. 根据gmail规范,他们将端口465用于SSL,而将端口587用于TLS。
Try either of below: 请尝试以下任一方法:
gmail.com.smtpPort = 587 gmail.com.smtpPort = 587
or 要么
gmail.com.starttlsEnable = false gmail.com.starttlsEnable = false
Hope this helps.......:) 希望这可以帮助.......:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.