Java - 窟僕喨周
<貧匯准
和匯准>
聞喘Java哘喘殻會窟僕E-mail噴蛍酒汽?徽頁遍枠低哘乎壓低議字匂貧芦廾JavaMail API 才Java Activation Framework (JAF) 。
才貧匯倖箭徨載屢貌?茅阻厘断勣聞喘setContent()圭隈栖宥狛及屈倖歌方葎"text/html"?栖譜崔坪否栖峺協勣窟僕HTML坪否。
- 低辛參壓 JavaMail (Version 1.2) 和墮恷仟議井云。
- 低辛參壅 壓JAF (Version 1.1.1)和墮恷仟議井云。
窟僕匯撃酒汽議 E-mail:
和中頁匯倖窟僕酒汽E-mail議箭徨。邪譜低議localhost厮将銭俊欺利大。
// File Name SendEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "abcd@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
園咎旺塰佩宸倖殻會栖窟僕匯撃酒汽議E-mail?
$ java SendEmail Sent message successfully....泌惚低?窟僕匯撃e-mail公謹倖辺周繁?椎担聞喘和中議圭隈栖峺協謹倖辺周繁ID?
void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException和中頁斤噐歌方議宙峰?
- type: 勣瓜譜崔葎TO, CC 賜宀BCC. 宸戦CC 旗燕貝僕、BCC 旗燕蜘畜貝僕y. 訟箭?Message.RecipientType.TO
- addresses: 宸頁email ID議方怏。壓峺協窮徨喨周ID扮?低繍俶勣聞喘InternetAddress()圭隈。
窟僕匯撃HTML E-mail:
和中頁匯倖窟僕HTML E-mail議箭徨。邪譜低議localhost厮将銭俊欺利大。才貧匯倖箭徨載屢貌?茅阻厘断勣聞喘setContent()圭隈栖宥狛及屈倖歌方葎"text/html"?栖譜崔坪否栖峺協勣窟僕HTML坪否。
// File Name SendHTMLEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendHTMLEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "abcd@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Send the actual HTML message, as big as you like
message.setContent("<h1>This is actual message</h1>",
"text/html" );
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
園咎旺塰佩緩殻會栖窟僕HTMLe-mail?
$ java SendHTMLEmail Sent message successfully....
窟僕揮嗤現周議 E-mail:
和中頁匯倖窟僕揮嗤現周議 E-mail議箭徨。邪譜低議localhost厮将銭俊欺利大。
// File Name SendFileEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendFileEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "abcd@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("This is message body");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart );
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
園咎旺塰佩低議殻會栖窟僕匯撃揮嗤現周議喨周。
$ java SendFileEmail Sent message successfully....
喘薩範屬何蛍:
泌惚俶勣戻工喘薩兆才畜鷹公e-mail捲暦匂栖器欺喘薩範屬議朕議?低辛參宥狛泌和譜崔栖頼撹?
props.setProperty("mail.user", "myuser");
props.setProperty("mail.password", "mypwd");
e-mail凪麿議窟僕字崙才貧峰隠隔匯崑。
<貧匯准
和匯准>