Send java email without authentication/with out sender password - java

I'm trying to send java email without senders password. Below you can see the code. I change mail.smtp.auth from true to false. There is a override method which method name getPasswordAuthentication():
public class SendMailUtilNineThirty implements Runnable {
final private String SMTP_SERVER = "192.186.16.14";
final private String SMTP_PORT = "135";
final private String TO_EMAIL = "saman#thal.com";
final private String TO_EMAIL = "dumidu#thal.com";
final private String FROM_EMAIL = "samanchandana#thal.com";
final private String FROM_EMAIL_PASSWORD = "1qaz2wsx#";
public void sendEmail(String emailContent, String subject) {
try {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_SERVER);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "false");
Session mailSession = Session.getInstance(props, new Authenticator() {
#Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM_EMAIL, FROM_EMAIL_PASSWORD);
}
});
mailSession.setDebug(true);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(FROM_EMAIL));
message.addHeader("site", "thal.com");
message.addHeader("service", "Thal Service");
message.setSentDate(new Date());
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(TO_EMAIL));
message.setSubject(subject);
How to send email without password authentication? Thanks.

In general, you need to authenticate to the mail server. If you run your own mail server, you may be able to set it up so that it doesn't require authentication, but none of the public email service vendors is going to allow that, for what I hope are obvious reasons.

Related

Formatting an email with JavaMail in Java

So I have sent an email in Java, however It doesn't format the email as I'd like. Instead of a formatted email like this:
Hello John,
Welcome to X.
Thanks,
Johnathan.
It would show:
Hello John, Welcome to X. Thanks, Johnathan.
This is the code:
public class MailReceipt {
String receipt;
String email;
public MailReceipt(String message, String email) {
this.receipt = message;
this.email = email;
}
public void sendMessage() {
final String username = "abc123#gmail.com";
final String password = "abc123";
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");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(email));
message.setSubject("DO NOT REPLY: A receipt regarding your recent purchase.");
message.setContent(receipt, "text/html; charset=utf-8");
Transport.send(message);
System.out.println("The mail was sent");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
I am using '\n' tags in the string, however it doesn't seem to work here. Would I need to make use of html in Java? If so, could I get some examples?
You've said its content-type is HTML, so use HTML. A newline isn't significant in HTML. Try <p>, or <br/>.

java: Sending failed

After multiple researches I can't succeed to send any mail with java
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("matr#live.fr"));
InternetAddress toAddress = new InternetAddress("matrphone#gmail.com");
msg.addRecipient(Message.RecipientType.TO, toAddress);
msg.setSubject("sujet du mail de text");
msg.setText("aaa");
Transport transport = session.getTransport("smtps");
transport.connect("smtp.gmail.com", 587,"matr#live.fr", "mypasswordcached");
transport.send(msg);
I have Exception in thread "main" javax.mail.NoSuchProviderException: No provider for smtps
I don't understand why
Anyone can help me please ?
Try the following code:
package org.kodejava.example.mail;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
public class GmailSendEmailTLS {
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static void main(String[] args) throws Exception {
//
// Email information such as from, to, subject and contents.
//
String mailFrom = "email#gmail.com";
String mailTo = "email#gmail.com";
String mailSubject = "TLS - Gmail Send Email Demo";
String mailText = "TLS - Gmail Send Email Demo";
GmailSendEmailTLS gmail = new GmailSendEmailTLS();
gmail.sendMail(mailFrom, mailTo, mailSubject, mailText);
}
private void sendMail(String mailFrom, String mailTo,
String mailSubject, String mailText)
throws Exception {
Properties config = createConfiguration();
//
// Creates a mail session. We need to supply username and
// password for Gmail authentication.
//
Session session = Session.getInstance(config, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
GmailSendEmailTLS.USERNAME,
GmailSendEmailTLS.PASSWORD
);
}
});
//
// Creates email message
//
Message message = new MimeMessage(session);
message.setSentDate(new Date());
message.setFrom(new InternetAddress(mailFrom));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailTo));
message.setSubject(mailSubject);
message.setText(mailText);
//
// Send a message
//
Transport.send(message);
}
private Properties createConfiguration() {
return new Properties() {{
put("mail.smtp.auth", "true");
put("mail.smtp.host", "smtp.gmail.com");
put("mail.smtp.port", "587");
put("mail.smtp.starttls.enable", "true");
}};
}
}
This example taken from: Sending email using gmail via TLS

GAE not working for send mail using Send Grid using java

I'm trying to send a mail using SendGrid by deploying my code to GAE. Following is my code.
private static final String SMTP_HOST_NAME = "smtp.sendgrid.net";
private static final String SMTP_AUTH_USER = "*******";
private static final String SMTP_AUTH_PWD = "*******";
private static final int SMTP_PORT = 2525;
public void sendCustomer(String userName, String toEmail, int custId) {
try {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
Multipart multipart = new MimeMultipart("alternative");
// Sets up the contents of the email message
BodyPart part1 = new MimeBodyPart();
part1.setText("Hello "
+ userName
+ "\n\n\n\n"
+ "Welcome to NotionViz. You have been registered successfully in NotionViz.");
multipart.addBodyPart(part1);
message.setText("UTF-8", "html");
message.setContent(multipart);
message.setFrom(new InternetAddress(SMTP_AUTH_USER));
message.setSubject("Customer Registration");
InternetAddress internetAddress = null;
try {
internetAddress = new InternetAddress(toEmail);
internetAddress.validate();
} catch (Exception e) {
System.out.println("Not a valid email address");
}
message.addRecipient(Message.RecipientType.TO, internetAddress);
InternetAddress address = new InternetAddress("cloud.spaninfotech#gmail.com");
message.setFrom(address);
// Sends the email
transport.connect(SMTP_HOST_NAME, SMTP_PORT, SMTP_AUTH_USER,
SMTP_AUTH_PWD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.sendMessage(message,
message.getFrom());
transport.close();
} catch (Exception e) {
}
}
// Authenticates to SendGrid
class SMTPAuthenticator extends javax.mail.Authenticator {
#Override
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
This program is working fine and sending mail in local. But if i deploy to GAE and check, I'm not getting an email. Please let me know why GAE restricting third party mail sending.
Try changing the port that you're using. You can hit SendGrid over port 587, 25 or 2525 for plain/TLS connections (465 if you were going to be using SSL).
SendGrid suggests port 587 to avoid rate limits set by some hosting companies so I would give that a shot.
On GAE sockets are a subject to a series of restrictions (see Sockets API documentation). Authenticated SMTP is allowed on submission port 587, so you can use it as already suggested by LaCroixed.

Java email - show multiple email recipients?

Is it possible to use Java to send an email such that I can see multiple recipients in the to/cc/bcc fields?
In other words, something like this:
From: foo#bar.com
To: user1#lol.com; user2#lol.com; user3#lol.com; user4#lol.com
Cc: admin1#lol.com; admin2#lol.com
I searched on Google but found no conclusive results, so any advice would be much appreciated.
Yes it is!
check out the javax.mail library.
Here is a code example:
class EmailSender{
private Properties properties;
private Session session;
private Message msg;
private final String SENDER_EMAIL = "your.email#whatever.com";
private final String PWD = "***********";
public void sendMail(String body) throws Throwable{
initMail();
msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("SENDER_EMAIL"));
//HERE YOU CAN CHOOSE BETWEEN TO, CC & BCC
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("Receiver Email"));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("Receiver Email2"));
msg.setRecipient(Message.RecipientType.CC, new InternetAddress("CC Email"));
msg.setRecipient(Message.RecipientType.CC, new InternetAddress("CC Email2"));
msg.setSubject("SUBJECT");
msg.setText(body);
//TRANSPORT
Transport.send(msg);
System.out.println("message sent!");
}
private void initMail(){
//PROPERTIES
//I choosed GMAIL for the demonstration, but you cant choose whatever you want.
properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
//AUTHENTICATION
session = Session.getInstance(properties, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SENDER_EMAIL, PWD);
}
});
}
}
Don't forget to import the javax.activation library.
Yes it is.
How to do it - depends on the library you're using.

javax authentication failed

I am trying out an email app on android.I take the username and password first
then on the next activity i take the recipiend address subject and msg and send the email.
I get the error JavaX authentication failure.Here is my Mail authentication and sending code.OnClick function of the send button calls this class
public class MailHandler {
final String username;
final String password;
public MailHandler(String username,String password){
this.username=username;
this.password=password;
}
public void sendMail(String Sub,String msg,String sender,String cc,String to) {
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");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(Sub);
message.setText(msg);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
A simple solution is to use the final String parameters, such as
public void sendMail(final String username ,final String password );.
Your code works. I copied it compiled and ran it using my gmail credentials. Are you sure you are using the correct gmail credentials for sending mail?
MailHandler m = new MailHandler("myAccount#gmail.com", "myPassword");
m.sendMail("Test", "Testing", "myOtherAccount#gmail.com", "myWork#myCompany.com", "myOtherWork#myCompany.com");
// After running I checked my work email and saw the message.
If I use an invalid password I get the following statcktrace. Is this what you are seeing
535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 jl8sm4152932obb.18
at MailHandler.sendMail(MailHandler.java:50)
at MailHandler.main(MailHandler.java:56)
Caused by: javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 jl8sm4151848obb.18
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:823)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:673)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at MailHandler.sendMail(MailHandler.java:45)
... 1 more

Categories