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

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));

Related

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(...));

Send Signature in Mail 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

Include Image in an email without attachments using java

I want to add an image to an email along with some text without having to attach the image in the email. Would this be possible ?
I have managed to do the following so far but it is sent with an attachment and also the image in the mail. I want it without an attachment but as a part of the mail
`Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(emailID));
message.setSubject("Password Reset");
Multipart multipart = new MimeMultipart("related");
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<html><body>Hi<img src=\"cid:the-img-1\"/></body></html>", "text/html");
multipart.addBodyPart(htmlPart);
BodyPart imgPart=new MimeBodyPart();
// Loading the image
DataSource ds=new FileDataSource("C:\\Users\\XYZ\\Desktop\\images.jpg");
imgPart.setDataHandler(new DataHandler(ds));
//Setting the header
imgPart.setHeader("Content-ID","the-img-1");
multipart.addBodyPart(imgPart);
// attaching the multi-part to the message
message.setContent(multipart);
Transport.send(message);`
You need to construct a multipart/related message.
You will be need to construct an html based e-mail for that purpose.
There are plenty of examples that you can get on google for that.
One example is look at here.
Note that image you want to send should be accessible over http to recipient. May be you can upload it on google drive or some image server

Java add header in MimeMessage msg

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.

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