Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
parallels committed Oct 2, 2018
0 parents commit a09ed59
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/SendEmail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendEmail {

public static void main(String[] args) {
//authentication info
final String username = "yourUsername@email.com";
final String password = "password";
String fromEmail = "fromemail@yahoo.com";
String toEmail = "toEmail@example.com";

Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.mail.yahoo.com");
properties.put("mail.smtp.port", "587");

Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
//Start our mail message
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(fromEmail));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
msg.setSubject("Subject Line");

Multipart emailContent = new MimeMultipart();

//Text body part
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("My multipart text");

//Attachment body part.
MimeBodyPart pdfAttachment = new MimeBodyPart();
pdfAttachment.attachFile("/home/parallels/Documents/docs/javamail.pdf");

//Attach body parts
emailContent.addBodyPart(textBodyPart);
emailContent.addBodyPart(pdfAttachment);

//Attach multipart to message
msg.setContent(emailContent);

Transport.send(msg);
System.out.println("Sent message");
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

0 comments on commit a09ed59

Please sign in to comment.