Send Signature in Mail JAVA - java

I am sending a mail in GWT using Google Oauth Authentication and token.
My Project use Google OAuthAuthentication.
The mail is send successfully . I just want to automatically include signature like we do in our mails while sending the mail.
I am able to send mail :
SMTPTransport transport = connectToSmtp(from, token);
Message msg = new MimeMessage(session);
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(toMail));
msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(ccMail));
msg.setFrom(new InternetAddress(from));
msg.setSubject("Imprest Report");
msg.setContent(messageBody ,"text/html; charset=utf-8");
transport.sendMessage(msg,msg.getAllRecipients());

You would have to add the signature as part of the "content" since I don't think email APIs supports signature as a concept

Related

AWS SNS Response Link to JavaMail

I have sent my mail through java mail and registered my email on Amazon to get the response notifications (Bounce, Delivered) etc.
How can i succesfully match these two to say when i have sent the mail, the mail i sent has the following incomming response.
Note: Everything is setup (Webhooks to get the response from AWS SNS)
I tried matching it by the messageId, but it seems aws adds a different messsageId than the java mail one. Example
MimeMessage msg = new MimeMessage(session);
msg.getMessageID(); // returns "<1619401941.3.1560500581268#Tinus-NB>"
And the from the AWS SNS response
"MessageId" : "7cd42bc4-e5c2-576b-a567-7eb9baa51cad" // directly
"Message" : "{\"notificationType\":\"Delivery\",\"mail\":{\"timestamp\":\"2019-06-14T08:38:55.113Z\",\"messageId\":\"0102016b5523c189-55acd572-ba3f-4750-aaae-b7019080f1ae-000000\",\"delivery\":{\"timestamp\":\"2019-06-14T08:39:03.043Z\",\"processingTimeMillis\":7930,\"smtpResponse\":\"250 2.0.0 OK 1560501543 f15si1572805ede.113 - gsmtp\"}}" // this is in the Message object in the JSON
Just to be clear, I want to match these two, to know what i sent and what the response was.
I am using Java Mail and not the AWS SDK lib
SES overwrites MessageID whatever you set because it also needs to know which email is it. In your case, when sending emails from Javamail,in the last section of your code, you can print the 250 response code+ message ID from SES and match it with the message ID you received from SNS.
When SES accepts an email in SMTP conversation, it gives 250 ok+ message ID and the same message ID can be seen in SNS notification.
Found it, i just had to include the original headers

Cannot send email without authentication required

I wrote a simple Java program that uses Java Mail API to send an email.
public static void main(String[] args) {
System.out.println("SimpleEmail Start");
String smtpHostServer = "smtp.gmail.com";
String emailID = "xxxxxx#hotmail.com";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHostServer);
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, null);
EmailUtil.sendEmail(session, emailID,"SimpleEmail Testing Subject", "SimpleEmail Testing Body");
}
}
EmailUtil class:
public class EmailUtil {
/**
* Utility method to send simple HTML email
* #param session
* #param toEmail
* #param subject
* #param body
*/
public static void sendEmail(Session session, String toEmail, String subject, String body){
try
{
MimeMessage msg = new MimeMessage(session);
//set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress("no_reply#example.com", "NoReply-JD"));
msg.setReplyTo(InternetAddress.parse("no_reply#example.com", false));
msg.setSubject(subject, "UTF-8");
msg.setText(body, "UTF-8");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
System.out.println("Message is ready");
Transport.send(msg);
System.out.println("EMail Sent Successfully!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
With this implementation, it is said I do not need any password at all so i gave this a tried and it tells me:
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required
I got this code from an online resources and from what I read,it should be able to send without authentication.
So my question is, do I always need to set a username and password to send mail using JAVA mail?
If no, what am I doing wrong?
It depends on who's running the server.
See https://support.google.com/a/answer/176600?hl=en&vid=0-974788924023-1554173451081; specifically the column labeled 'Gmail SMTP server' (which you're using). Google explicitly says that you have to authenticate to utilize that server. Not doing so gives you this error message.
Once upon a time you could use any mail server to send emails to any email address you wanted.
And then came SPAM. Spammers also could use any mail server to send emails to any email address - and as a mail server operator you do not want spammers to use your server for sending emails (because operating the server costs you money, because your mail server can get blacklisted for spamming).
So today most mail servers require that you
either provide authentication (for sending emails to any email address you want)
or are only allowed to send emails to email addresses hosted by the mail server operator
Google even has two distinct mail servers:
one that requires authentication, that can be used for sending emails to any email address you want (that is the server at smtp.gmail.com)
one that doesn't require authentication, that can only be used for sending emails to Gmail or G Suite users (that is the server aspmx.l.google.com)
It could be that your source dates back to a time when no authentication was required or that the mail server in your source was only used for sending mails to addresses that are hosted at the mail server.
Either way - if you want to use the server smtp.gmail.com for sending mails to any address you must authenticate (or convince google that they should allow you - and only you - to sending emails without authentication, but then: how will google know that it is exactly you who is trying to send mails?)

AppEngine Email : Unauthorized Sender

When I try to send mail I get a "Unauthorized sender" exception
javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Illegal Arguments (java.lang.IllegalArgumentException: Unauthorized Sender: Unauthorized sender))
at javax.mail.Transport.send(Transport.java:163)
at javax.mail.Transport.send(Transport.java:48)
My code to send mail is very simple:
Session session = Session.getDefaultInstance(new Properties(), null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("admingae#tecurti.com", "Adming"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("name#gmail.com", "Felipe"));
msg.setSubject("Assunto", "UTF-8");
msg.setText("texto corpo");
Transport.send(msg);
I´ve already give to admingae#tecurti.com "owner" permission on admin console.
Here is my App Engine Console permission
If anyone could help me I appreciated
thanks
Solutions
Thanks to Andrei Volgin I will register the solution
Admin Console Correct Register
In console go to App Engine > Settings > Application Settings. Add this email address to the list of authorized senders.
Today this is little different and requires more setup for security reasons.
The zero configuration way, is sending email from an email address with this format:
[anything]#[project_id].appspotmail.com
FYI: https://cloud.google.com/appengine/docs/standard/java/mail/#who_can_send_mail

unable to send mails to outlook 2007 using on behalf of

Our application uses javax.mail.MimeMessage, we are written this code
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress("mail#xyz.com", "XYZ"));
From section changing work in gmail and yahoo but not working outlook,
can any tell reason for this.
You need to add msg.setSender(new InternetAddress(...));

Sending Mail With Javax but receiver "MAILER-DAEMON" as sender

I am trying to use javax to send emails programmatically.
I followed steps on answer of the following question: Click here and it works fine. I just changed mail server to "smtp.yandex.com.tr"
When i send/receive email with this code it writes "MAILER-DAEMON" in sender section. How can I fix this?
By the way I make also authentication with a yandex mail to send.
I fixed this with adding from part. I was thinking just giving sender was ok but apperantly i had to specify "from" as follows
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setFrom(new InternetAddress(sender));

Categories

Resources