user3479640
user3479640

Reputation: 289

Spring Java Mail - How to know send mail be read, bounce or forward by receiver?

I'm new on spring and java mail, may i know how the email send by java mail is read, bounce or forward. I used google search only found the way how to send a mail. Anyone know where can get the reference on it or provide me some example on it?

Thank you.

Below is my code send mail with spring java mail:

Spring-Mail.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="[email protected]" />
    <property name="password" value="xxxxxx" />

    <property name="javaMailProperties">
       <props>
              <prop key="mail.smtp.auth">true</prop>
              <prop key="mail.smtp.starttls.enable">true</prop>
              <prop key="mail.smtp.from">[email protected]</prop>
           </props>
    </property>
</bean>

<bean id="mailMail" class="com.penril.my.MailMail">
    <property name="mailSender" ref="mailSender" />
</bean>

</beans>

MailMail.java

public class MailMail
{
    private JavaMailSender mailSender;

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void sendMail(String from, String to, String subject, String msg) {

        MimeMessage message = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(msg);

            mailSender.send(message);

        } catch (MessagingException e) {
            throw new MailParseException(e);
        }
    }
}

MainClass.java

public class MainClass
{
    public static void main( String[] args )
    {
        ApplicationContext context = 
             new ClassPathXmlApplicationContext("Spring-Mail.xml");

        MailMail mm = (MailMail) context.getBean("mailMail");
        mm.sendMail("[email protected]",
               "[email protected]",
               "Testing123", 
               "Testing only \n\n Hello Spring Email Sender");

    }
}

Upvotes: 0

Views: 3690

Answers (2)

Alan Hay
Alan Hay

Reputation: 23246

Sign up for a free account for a Cloud Based SMTP service, for example Sendgrid, which will save you the trouble of manually implementing what was suggested in the previous answer.

https://sendgrid.com/

You can send 400 emails a day on the free tier. You can manually check the status of individual messages 'opened', 'bounced' etc using the management console or there are various APIs available to do this programatically (although some are only available on paid tiers).

For example their WebHooks API will call back your server when an event (opened, bounced, click etc.) occurs:

https://sendgrid.com/docs/API_Reference/Webhooks/event.html

Upvotes: 0

Mayank Pandya
Mayank Pandya

Reputation: 1623

There is no standard way of doing this that's accepted and honored across the board. I see that you have some options, though:

Add a header "Return-Receipt-To" with your e-mail address in the value. If the recipient of the e-mail has a client which honors this header, then a return receipt will be sent to you when the e-mail is opened. This is not reliable, mind you, as the user can always decide not to send the receipt, even if he has a client that supports it.

Add an image into your e-mail that loads from your server and put a parameter on the image that includes the user's e-mail address. When the e-mail loads, the image will load from your server. Write a script that collects the e-mail parameter and then delivers a blank image. This is also not reliable, however, as many mail clients prompt users if they wish to download images and they can always choose not to. Also, some (mostly older) e-mail clients do not support images.

Perhaps the most reliable way is not to include the message in your e-mail at all. Include only a link to a website where the message can be read, and include their e-mail address or a unique code in the link. This way, you know exactly who read the message. Of course, this has the downside that people aren't actually getting the message in their inbox, and they also may choose not to go to the website to read it.

Ultimately, I think you're going to have to come up with a creative solution to solve this problem, unless you're happy getting spotty results.

Upvotes: 1

Related Questions