forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.html
More file actions
105 lines (81 loc) · 3.71 KB
/
Copy pathjava.html
File metadata and controls
105 lines (81 loc) · 3.71 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
layout: page
weight: 0
title: Java
navigation:
show: true
---
<p>The easiest way to add SendGrid to your Java project is to use the <a href="http://github.com/sendgrid/sendgrid-java">sendgrid-java library</a>.</p>
{% codeblock lang:java %}
import com.github.sendgrid.SendGrid;
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
sendgrid.addTo("example@example.com");
sendgrid.setFrom("other@example.com");
sendgrid.setSubject("Hello World");
sendgrid.setText("My first email through SendGrid");
sendgrid.send();
{% endcodeblock %}
{% anchor h3 %}
Installation
{% endanchor %}
<p>There are multiple ways to install this library. Please see the <a href="https://github.com/sendgrid/sendgrid-java#installation">sendgrid-java installation instructions</a>.</p>
{% anchor h3 %}
Usage
{% endanchor %}
<p>For detailed usage of the library, please see the <a href="https://github.com/sendgrid/sendgrid-java#usage">sendgrid-java detailed usage instructions</a>. You can do things like add attachments, adjust heads, set multiple TO addresses and more.</p>
{% anchor h2 %}
JavaMail Approach
{% endanchor %}
<p>If you prefer, you can use <a href="http://javamail.java.net/nonav/docs/api/javax/mail/internet/package-summary.html">JavaMail</a> instead of the <a href="http://github.com/sendgrid/sendgrid-java">sendgrid-java library</a> to send emails through SendGrid.</p>
<p>Below is an example using JavaMail to build a multi-part MIME email and send it through SendGrid.</p>
{% codeblock lang:java %}
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
public class SimpleMail {
private static final String SMTP_HOST_NAME = "smtp.sendgrid.net";
private static final String SMTP_AUTH_USER = "myusername";
private static final String SMTP_AUTH_PWD = "mypwd";
public static void main(String[] args) throws Exception{
new SimpleMail().test();
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
// uncomment for debugging infos to stdout
// mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
Multipart multipart = new MimeMultipart("alternative");
BodyPart part1 = new MimeBodyPart();
part1.setText("This is multipart mail and u read part1......");
BodyPart part2 = new MimeBodyPart();
part2.setContent("<b>This is multipart mail and u read part2......</b>", "text/html");
multipart.addBodyPart(part1);
multipart.addBodyPart(part2);
message.setContent(multipart);
message.setFrom(new InternetAddress("me@myhost.com"));
message.setSubject("This is the subject");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("someone@somewhere.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
{% endcodeblock %}