Use the below snippet to send email, programmatically. You could use this to send your reports after execution.
package mslt.verification; |
import java.util.Properties; |
import javax.activation.DataHandler; |
import javax.activation.DataSource; |
import javax.activation.FileDataSource; |
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; |
import org.testng.annotations.Test; |
public class Sendemail { |
@Test |
public void sendemailWithAttachments() { |
final String username = "YOUR USER NAME"; |
final String password = "YOUR PASSWORD"; |
Properties props = new Properties(); |
props.put("mail.smtp.auth", "true"); |
props.put("mail.smtp.starttls.enable", "true"); |
props.put("mail.smtp.host", "smtp.gmail.com"); |
props.put("mail.smtp.port", "587"); |
Session session = Session.getInstance(props, |
new javax.mail.Authenticator() { |
protected PasswordAuthentication getPasswordAuthentication() { |
return new PasswordAuthentication(username, password); |
} |
}); |
try { |
Message message = new MimeMessage(session); |
message.setFrom(new InternetAddress(username)); |
message.setRecipients(Message.RecipientType.TO, |
InternetAddress.parse("STEVE@APPLE.HEAVEN")); |
message.setSubject("THANK YOU"); |
message.setText("PFA"); |
MimeBodyPart messageBodyPart = new MimeBodyPart(); |
Multipart multipart = new MimeMultipart(); |
messageBodyPart = new MimeBodyPart(); |
String file = "/Users/admin/Documents/THANKYOU.NOTE"; |
String fileName = "THANKYOU.NOTE"; |
DataSource source = new FileDataSource(file); |
messageBodyPart.setDataHandler(new DataHandler(source)); |
messageBodyPart.setFileName(fileName); |
multipart.addBodyPart(messageBodyPart); |
message.setContent(multipart); |
System.out.println("Sending"); |
Transport.send(message); |
System.out.println("Done"); |
} catch (MessagingException e) { |
e.printStackTrace(); |
} |
} |
} |
No comments:
Post a Comment