Forwarding SMS Messages to Email Using Java
Overview
Businesses get communications through many channels. It can be handy to have a searchable archive of messages in one place. Forwarding SMS messages to email lets you keep both kinds of messages in one spot. Plivo makes it easy to forward SMS messages to email using the most popular web development languages. Here we walk through the process with .NET.
Prerequisites
Plivo account: Sign up for a Plivo account if you don’t have one already.
Plivo phone number: To receive SMS, you must have a Plivo phone number that supports SMS. You can purchase numbers from the Numbers page of the Plivo console or by using the Numbers API.
The code example below presumes you have a Gmail account, but it’s easy to edit the code to support another SMTP client.
Install Java and other modules
You must set up and install Java(Java 1.8 or higher) along with other modules to Forward SMS to Email. Here’s how.
Install Java
Operating System | Instructions |
---|---|
macOS and Linux | To see if you already have Java installed, run the command java -version in the terminal. If you do not have it installed, you can install it from here. |
Windows | To install Java on Windows follow the instructions listed here. |
Install Spring and other modules using IntelliJ Idea
- Use Spring Initializr to create a boilerplate project with Spring Boot framework.
- Choose the “Spring Web” dependency, Give the project a friendly name and click “Generate” to download the boilerplate code and open it in IntelliJ Idea.
Install the Java Mail package by adding the dependency in
pom.xml
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
Forward SMS to Email
Below is the code to Forward Incoming SMS to Email.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.example.plivo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import javax.mail.*;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
@SpringBootApplication
@RestController
public class PlivoApplication {
public static void main(String[] args) {
SpringApplication.run(PlivoApplication.class, args);
}
@PostMapping("/email_sms")
public void postBody(String From, String To, String Text) {
System.out.println(From + " " + To + " " + Text);
final String username = "<email_address>";
final String password = "<password>";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true"); //TLS
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("<from_email_addres>"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("<recipient_email_address>")
);
message.setSubject("SMS from" + From);
message.setText(Text);
Transport.send(message);
System.out.println("Email Sent!");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Save the file and run it.
Exposing your local server to the internet
To receive Incoming Messages and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.
Install ngrok and run it on the command line, specifying the port that hosts the application on which you want to receive messages (8080 in this case):
$ ./ngrok http 8080
Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.
Create an Application
- Create a Plivo application by visiting Messaging > Applications and clicking on
Add New Application
, or by using Plivo’s Application API. - Give your application a name — we called our
Email SMS
. Enter your server URL (for example https://349f5c931df4.ngrok.io/email_sms/) in theMessage URL
field and set the method asPOST
. - Click on
'Create Application'
to save your application.
Assign a Plivo number to your app
- Navigate to the Numbers page and select the phone number you want to use for the application.
- Select XML Application from the Application Type drop-down list, and
Email SMS
(the name of the application) from the Plivo Application drop-down list. - Click on
Update Number
to save.
Test and validate
Then send a text message to the Plivo number you associated with the application using a regular mobile phone.The incoming message should be reflected in your email.