0% found this document useful (0 votes)
22 views3 pages

EX5 Gmail Spring - Docx-1

This document provides an example of how to send an email with an attachment using Spring. It includes Java classes for sending mail, an XML configuration file, and a test class to send a test email.

Uploaded by

Pranav Vernekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

EX5 Gmail Spring - Docx-1

This document provides an example of how to send an email with an attachment using Spring. It includes Java classes for sending mail, an XML configuration file, and a test class to send a test email.

Uploaded by

Pranav Vernekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercise 5:

Write a program to send email with attachment using spring.

MailMail.java
package com.javatpoint;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class MailMail{


private MailSender mailSender;

public void setMailSender(MailSender mailSender) {


this.mailSender = mailSender;
}

public void sendMail(String from, String to, String subject, String msg) {
//creating message
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(msg);
//sending message
mailSender.send(message);
}
}

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">


<property name="host" value="smtp.gmail.com" />
<property name="username" value="[email protected]" />
<property name="password" value="kyjv ikpl nufs lvyl" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.port">465</prop>
</props>
</property>
</bean>
<bean id="mailMail" class="com.javatpoint.MailMail">
<property name="mailSender" ref="mailSender" />
</bean>
</beans>

Test.java
import com.javatpoint.MailMail;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {


public static void main(String[] args) {
try {
Resource r = new ClassPathResource("applicationContext.xml");
BeanFactory b = new XmlBeanFactory(r);
MailMail m = (MailMail) b.getBean("mailMail");
String sender = "[email protected]"; //write here sender gmail id
String receiver = "[email protected]"; //write here receiver id
m.sendMail(sender, receiver, "hi", "welcome");

System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
}
}

You might also like