I
I
insomnia772015-11-06 12:00:51
Java
insomnia77, 2015-11-06 12:00:51

Java. Sending a letter through gmail host with proxy authentication by login and password?

Good afternoon. Couldn't find a working example on stackoverflow of sending emails using java.
I wrote this code, but it hangs. Can anyone tell me where is the error in this method?
public void SendEmail(String SendGmailTo, String subject, String text, String SendGmailFrom, String SendGmailFromPassword, String proxyHost, String proxyPort, String proxyUsername, String proxyPassword) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.socks.
props.put("mail.smtp.socks.port", proxyPort);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SendGmailFrom, SendGmailFromPassword);
}
});
System.setProperty("java.net.socks.username", proxyUsername);
System.setProperty("java.net.socks.password", proxyPassword);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(SendGmailFrom));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(SendGmailTo));
message.setSubject(subject);
message.setText(text);
transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Agarkov, 2015-11-12
@stas_agarkov

public static void sendMail0(String smtpServer, String smtpPort, final String smtpLogin, final String smtpPassword, String emailRecipients, String emailSubject, MessageSetter messageSetter) {
try {
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", smtpServer);
properties.setProperty("mail.smtp.port", smtpPort);
properties.setProperty("mail.smtp.socketFactory.port", smtpPort);
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpLogin, smtpPassword);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(smtpLogin));
for (String recipient : StringUtils.split(emailRecipients, ",")) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
message.setSubject(emailSubject, "UTF-8");
messageSetter.set(message);
transport.send(message);
log.debug("Email successfully sent to addresses: " + emailRecipients);
} catch (MessagingException e) {
log.debug("MessagingException", e);
}
}
new MessageSetter() {
@Override
public void set(MimeMessage mimeMessage) throws MessagingException {
mimeMessage.setContent(emailHtmlText, "text/html; charset=UTF-8");
}
}
This code works for me.

V
Vladimir Smirnov, 2015-11-12
@bobzer

I would advise you to start by making the code work in an environment that does not have a proxy server, because it is not clear with whom to "fight" - with the mail server or with the proxy. Recently "fought" with Gmail, here are my working settings:

mail.smtp.host              smtp.gmail.com
mail.smtp.socketFactory.port	 465
mail.smtp.socketFactory.class	 javax.net.ssl.SSLSocketFactory
mail.smtp.auth                         true
mail.smtp.port                         465
mail.sender.email                    [email protected]
mail.sender.email.password    mypassword
mail.smtp.ssl.enable                true
mail.smtp.ssl.trust                   *

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question