Java add header in MimeMessage msg - java

Problem that i'm solving is that when i send mail if the recipient (to mails, cc/bcc) not exist, i don't want sender to get Delivery Status Notification (Failure) mail.
The solution that i'm implementing is adding new header in mail Prevent-NonDelivery-Report
I want to add new header in MimeMessage msg in java
// this is part of the code that defined the mime message
Session session = Session.getDefaultInstance(props, null);
// we create message
Message msg = new MimeMessage(session);
// this code is not working for me
msg.addHeader("Prevent-NonDelivery-Report", "");
I found the solution i add props.setProperty("mail.smtp.dsn.notify", "NEVER") to the property of the session and that fix my problem

On behalf of the OP:
I found the solution. I add:
props.setProperty("mail.smtp.dsn.notify", "NEVER")
to the property of the session and that fix my problem.

Related

Java Mail is it possible to show users email in from field?

I am creating a contact form and was wondering if it is possible to show the user's email in the "from "field in a contact email box. I am using java mail api.
Now it looks like this
can I show users' emails instead of grazerteamroma#gmail.com?
It seems like a can`t get access to a user's to account to send the mail from their account, am I right?
Not possible to set the sender address without the email server/provider.
Try replyTo Field in your code.
you can try replyTo of MimeMessage class.
Here is the spring version MimeMessageHelper class, you can set it using MimeMessage also.
public void sendEmail(Mail mail)
{
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setSubject(mail.getMailSubject());
mimeMessageHelper.setFrom(new InternetAddress(mail.getMailFrom()));
mimeMessageHelper.setTo(mail.getMailTo());
mimeMessageHelper.setText(mail.getMailContent());
mimeMessageHelper.setReplyTo(new InternetAddress("anupamXXXX#gmail.com"));
javaMailSender.send(mimeMessageHelper.getMimeMessage());
}
catch (MessagingException e) {
e.printStackTrace();
}
}
Using above code receiver will receive with replyto address on which he directly reply.
Java Mail is an API to talk to the backend (Mail User Agent to Mail Transport Agent communication). It is not related to presenting the mails to users at all.
So of course it is possible to users' email on the screen, be it the sender, the recipient or else. But this is a UI rendering issue, not Java Mail.
In other words: You are barking up the wrong tree.
But since you are asking:
Send email - here are examples
Receive email - here are examples
In case your question is just about how Google Mail can print the name of the recipient independently of the email address, it probably parses this (recipient/originator) string:
Alfred Neuman <Neuman#BBN-TENEXA>
See the specification for email messages: https://www.w3.org/Protocols/rfc822/#z10

How to get the message id of the email message before sending using Exchange Web Service(JAVA)?

Need to track the emails so before sending emails I need to get the message id of the email message.
All you need is to save the EmailMessage first before you send it.
EmailMessage emailMessage = new EmailMessage(service);
emailMessage.save();
EWS saves the email message and assigns an Internet message identifier to it. But on your local emailMessage this property still isn't set. Trying to access it by
emailMessage.getInternetMessageId();
will result in a ServiceObjectPropertyException. You need to get the remote and updated version of the email message:
emailMessage = EmailMessage.bind(service, emailMessage.getId());
Now you can read the Internet message identifier property. Set other properties as needed and then send the message.

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 email in HTML format.

I am trying to send email in HTML format. It is working for text/plain. But when i set content type to text/html mail is not being transported(no exception is being thrown but i don't get email as well).
Following is my code.
public void postMail() throws Exception {
boolean debug = false;
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", smtpServerPort);
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");
lgr.debug(lgr.isDebugEnabled()?"SMTP Server --->" + smtpServer : null);
Session session = Session.getInstance(props, null);
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
// Here is some logic to add TO and CC and BCC
msg.setSubject(subject);
// writer.println("Subject : " + subject);
lgr.debug(lgr.isDebugEnabled()?"Subject : " + subject : null);
msg.setContent(message, "text/html");
Transport.send(msg);
lgr.info(lgr.isInfoEnabled() ?"Mail sent": null);
}
Any help in this regard will be highly appreciated.
Get rid of the socket factory stuff, you don't need it.
When you set debug to true, do you see any errors in the debug output? Or is your mail server accepting the message without complaint? If no errors, then most likely the problem is that the recipient's mail server thinks your message is spam, although that seems unlikely with your simple test message.
Have you tried sending to different recipients, especially different recipients using different mail servers? It would be useful to figure out if the problem is with the recipient's mail server or with the mail server you're using for sending.
I faced a similar issue some times back. I found that the issue was with the html content containing single quotes.
As a test try to send a (simple word)/(simple html without quotes at all) as message using text/html format. If that works you can probably modify your html message to contain only double quotes.
Hope that helps!

how to send email through outlook from java

i have using the following code for sending mail within a domain.
public void sendMail(String mailServer, String from, String to,
String subject, String messageBody, String[] attachments)
throws MessagingException, AddressException {
// Setup mail server
Properties props = System.getProperties();
props.put("mail.smtp.host", mailServer);
// Get a mail session
Session session = Session.getDefaultInstance(props, null);
// Define a new mail message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
// use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();
// add the message body to the mime message
multipart.addBodyPart(messageBodyPart);
// add any file attachments to the message
addAtachments(attachments, multipart);
// Put all message parts in the message
message.setContent(multipart);
// Send the message
Transport.send(message);
System.err.println("Message Send");
}
protected void addAtachments(String[] attachments, Multipart multipart)
throws MessagingException, AddressException {
for (int i = 0; i < attachments.length ; i++) {
String filename = attachments[i];
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
// use a JAF FileDataSource as it does MIME type detection
DataSource source = new FileDataSource(filename);
attachmentBodyPart.setDataHandler(new DataHandler(source));
// assume that the filename you want to send is the same as the
// actual file name - could alter this to remove the file path
attachmentBodyPart.setFileName(filename);
// add the attachment
multipart.addBodyPart(attachmentBodyPart);
}
}
but if with the same code i try to send an email outside the domain, say i am sending email from mjsharma#domain.com to mhsharma#gmail,com then it fails and gives me the following error. 550 5.7.1 Rcpt command failed: Mail denied due to site's policy
Am i missing something in the above code.
Please help me
I suspect that's not a problem in your code, but in your mail server (sendmail?) configuration. I would talk to whoever administers your mail infrastructure.
It's likely that your local mail server requires you to authenticate before sending mail out into the world. This is a common policy to prevent spammers from relaying their mail through it.
Assuming your machine has Outlook installed... have you tried using moyosoft's java outlook connector? it's pretty simple to use and passes through network restrictions because it connects to your Outlook application and then sends the mail, so any restriction on smtp ports or servers/proxy policies will be ignored if your Outlook client is working fine.
if you are doing it with command line server-side then i guess this answer is useless for you.
Source: i had similar problem (not same error code, more like an intranet restriction) and using this library solved my problem because of the posted above.

Categories

Resources