Ok, the first step is to make sure you're sending what you think you're sending.
After your call to Transport.send, add
msg.writeTo(newFileOutputStream("msg.txt"));
If you don't know how to read a raw MIME message, post it here.
If msg.txt contains the correct message, compare it with the message that's
received by the recipient. Use the "view message source" option in the mail
reader.
If they're the same, then either the message content isn't actually correct for
what you're trying to do, or the mail reader is ignoring your html "suggestions"
and displaying the message however it wants.
Hello,
I am trying to develop an email application. It consists on sending email from email1@domain.com to email2@email.com
How can I do this in java ?
I am using this software but it works only with gmail !!
/**
* Send Email to the webmaster
*/
String d_email = "email1@gmail.com",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "email2@yahoo.com",
m_subject = "Email from user";
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
//session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setText("This is a message from: "+UserEmail+"\n "+UserMessage);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.setSentDate(new Date());
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport.send(msg);
}
catch (Exception ex)
{
ex.printStackTrace();
out.println("Messaging ERROR: " + ex);
out.println(stack2string(ex));
if(ex.getMessage().compareTo("")!=0)
check = "Message NOT SENT " + ex.getMessage();
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return
new PasswordAuthentication("email1@gmail.com", "pwd");
}
}
please advice
I'm not sure which part you're confused about but I'm guessing that there's
something basic about how internet email works that you're not understanding.
In all cases, you need a mail server to give your message to. The mail server
will then take responsibility for routing the message to the correct destination
mail server. The gmail mail server works fine for this, assuming you have a
gmail account.
Gmail probably won't let you falsify the From address for your message so if
you want to send mail that appears to come from some address other than
your gmail account, you'll probably need to use a mail server associated with
the account you want the mail to appear to come from. If you run your own
mail server, you can decide whether it will allow senders to falsify the From
address.