i peek in many mail api like gmail etc. they send html content by mail api ,almost a form with some widgets. I am also trying to do that but whenever in try this with html content , its giving me error
String to="xyz#gmail.com";//change accordingly
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xyz.com","xyz");
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("hellofacebook180#gmail.com"));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(input);
message.setContent("<h1>sending html mail check</h1>","text/html" );;
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
return "massage sent";
please help how can I sent Html content with mail api
Email clients (Gmail, etc.) do not allow external CSS files. There is nothing you can do about it.
Related
I am trying to write a small java program that can send email from my corporate outlook account. I am planning to run this java program on my office machine only. Below is my program:
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(my_email_id, my_password);
}
});
MimeMessage mime = new MimeMessage(session);
try {
mime.setSender(new InternetAddress(emailid));
mime.setRecipient(Message.RecipientType.TO, new InternetAddress(emailid));
mime.setSubject("Test");
mime.setText("Testing");
Transport.send(mime);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am getting below error :
javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [BM1PR01CA0166.INDPRD01.PROD.OUTLOOK.COM]
I have tried removing the socketFactory properties as well, but no luck. Can anyone help me out with this one? I am guessing it might have something to do with proxy or any other security on my office machine, but I am not able to figure out what it is.
I'm using javax.mail package for mail sending,
while application in debug mode it's work fine and send every mail to my account, but when i create release application mail stop sending form application.
code is as given below:
Properties props = new Properties();
props.put("mail.smtp.user", "abc#xyz.com");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
//Creating a new session
session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc#xyz.com", "password#123");
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress("abc#xyz.com"));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress("abc#xyz.com"));
//Adding subject
mm.setSubject(subject);
//Adding message
mm.setText(message);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
Generally this problem happen because of proguard, If you add proguard into your project it'll stop some of functionality like this.
I did it by adding package javax in proguard-rules.pro file.
-keep class javax.** {*;}
this will add all javax methods's permission in proguard.
i want to use javamail so i test this code
public class Test_Mail {
public static void main(String [] args)
{
String to="xyz#gmail.com";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc#gmail.com","*****");
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc#gmail.com"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Hello");
message.setText("Testing.......");
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
}
but i have this error:
Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 gj16sm129363wic.24 - gsmtp
i tried many changes but it's the same error.
Because Google is such a large target for SPAM they have policies and practices in place to try and prevent as much SPAM as possible. It appears that your account may have been flagged by one of these policies and requires some manual intervention to get it working again.
Hi I have to send email in java.
The below has successfully worked for me.
public class SendMail {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxxx#gmail.com","xxxxx!1");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("krishnaveni.veeman#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("mercy.krishnaveni#gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Here I have mentioned gmail username and password. But I have to send email without using username and password in my code. How can I develop this. Please help me.
create a mail.properties file and put username and password into in that file. Use Properties to retrieve data from this file in your code.
This link could be useful to load Properties object
http://www.dzone.com/snippets/loading-property-file
I have a program where I have a number of users which each could be sending emails from different email accounts.
When I try to use JavaMail to send emails. They always get sent out by the account of the user who sent an email first.
user1 = new User("dummy-email#gmail.com", "dumpass12");
user2 = new User("second-dummy#gmail.com", "secondpass12");
user1.sendMail(toAddress, subject, body);
user2.sendMail(toAddress, subject, body);
Now when I do something like this, the second user will send a message but it will come from the SAME mailbox as user1 (i.e. both messages will come from dummy-email#gmail.com).
Can somebody explain to me why this is happening? Do I have to close the connection somehow? How can I send the two emails and have them come from the different accounts? Please help me.
Here is my code which actually sends the email connecting to the user's gmail account.
public void sendMail(String toAddress, String subject, String body){
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication(getUsername(),getPassword()); }
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(getUsername()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddress));
message.setSubject(subject);
message.setContent(body, "text/html");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
Replace Session.getDefaultInstance() with Session.getInstance(). To understand why, read the javadocs for those methods carefully.