Eclipse中的Java简单电子邮件程序 [英] Java Simple Email Program in Eclipse
问题描述
我想制作一个简单的程序,您可以从命令行发送电子邮件。我发现这个教程, http://www.tutorialspoint.com/java/java_sending_email。 htm ',但是下载没有。那么在哪里可以获得JavaMail API和Java Activation Framework(JAF),我将如何把它放在我的课程路径中。
基本上我正在寻找一个人来分解它,告诉我如何制作电子邮件程序。
我使用Eclipse luna。
请看这个例子。这个例子,简单地发送一个附件作为一个邮件。附件 quiz.txt
的内容如下:
什么是印度首都/新德里
泰姬陵在哪里?/阿格拉
这里是 SendMailExample.java
文件:
import java.util。* ;
import javax.activation。*;
import javax.mail。*;
import javax.mail.internet。*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SendMailExample {
private String from;
private String to;
private String subject;
private String messageBody;
private String fileName;
private String host;
私有属性;
私人MimeMessage消息;
private BodyPart messageBodyPart;
private Multipart multipart;
私有身份验证器;
public SendMailExample(){
from =sender@gmail.com;
to =recipient@gmail.com;
subject =主题测试;
messageBody =< html>< body>< h1> HAVE FAITH,AND STAY+
CALM :-)我和你在一起,OKAY: - )< / h1> ; /体>< / HTML>中;
fileName =quiz.txt;
host =smtp.gmail.com;
authentator = new SMTPAuthenticator();
properties = System.getProperties();
properties.put(mail.smtp.host,主机);
properties.put(mail.smtp.starttls.enable,true);
properties.put(mail.smtp.port,587);
properties.put(mail.smtp.auth,true);
private void sendMail(String from,String to,
String subject,String messageBody,String fileName){
try {
Session session = Session.getDefaultInstance(properties,authenticator);
message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messageBody,text / html);
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println(Message sending successfully ....);
} catch(Exception me){
me.printStackTrace();
}
}
private void performTask(){
sendMail(from,to,subject,messageBody,fileName);
}
public static void main(String [] args){
new SendMailExample().performTask();
}
}
/ **
* SimpleAuthenticator用于在SMTP服务器需要时执行简单身份验证
*。
* /
class SMTPAuthenticator extends Authenticator {
private static final String SMTP_AUTH_USER =example@gmail.com;
private static final String SMTP_AUTH_PASSWORD =somepassword;
public PasswordAuthentication getPasswordAuthentication(){
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PASSWORD;
返回新的PasswordAuthentication(用户名,密码);
}
}
您只需要这个 mail.jar 文件。
要编译,只需写入( mail.jar
存在于C:\install\java\mail\mail.jar`位置):
javac -classpath。; C:\install\java\mail\mail.jar SendMailExample.java
$要运行,请写:
java -classpath。; C:\\ \\ install\java\mail\mail.jar SendMailExample
这样做: - )
I want to make a simple program where you can send email from the Command Line. I found this tutorial, ' http://www.tutorialspoint.com/java/java_sending_email.htm ', however the downloads don't. So where can I get JavaMail API and Java Activation Framework (JAF) and how would i put it in my class path.
Basically Im looking for someone to break it down and show me how I could make an email program.
Im using Eclipse luna.
解决方案 Do have a look at this example. This example, simply sends one attachment as a mail. The contents of attachment quiz.txt
are as follows:
What is the Capital of India?/New Delhi
Where is the Taj Mahal?/Agra
Here is the SendMailExample.java
file:
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SendMailExample {
private String from;
private String to;
private String subject;
private String messageBody;
private String fileName;
private String host;
private Properties properties;
private MimeMessage message;
private BodyPart messageBodyPart;
private Multipart multipart;
private Authenticator authenticator;
public SendMailExample () {
from = "sender@gmail.com";
to = "recipient@gmail.com";
subject = "Subject Testing";
messageBody = "<html><body><h1>HAVE FAITH, AND STAY" +
" CALM :-) I AM WITH YOU, OKAY :-)</h1></body></html>";
fileName = "quiz.txt";
host = "smtp.gmail.com";
authenticator = new SMTPAuthenticator ();
properties = System.getProperties ();
properties.put ( "mail.smtp.host", host );
properties.put ( "mail.smtp.starttls.enable", "true" );
properties.put ( "mail.smtp.port", "587" );
properties.put ( "mail.smtp.auth", "true" );
}
private void sendMail ( String from, String to,
String subject, String messageBody, String fileName ) {
try {
Session session = Session.getDefaultInstance ( properties, authenticator );
message = new MimeMessage ( session );
message.setFrom ( new InternetAddress ( from ) );
message.addRecipient ( Message.RecipientType.TO,
new InternetAddress ( to ) );
message.setSubject ( subject );
multipart = new MimeMultipart ();
messageBodyPart = new MimeBodyPart ();
messageBodyPart.setContent ( messageBody, "text/html" );
multipart.addBodyPart ( messageBodyPart );
messageBodyPart = new MimeBodyPart ();
DataSource source = new FileDataSource ( fileName );
messageBodyPart.setDataHandler ( new DataHandler ( source ) );
messageBodyPart.setFileName ( fileName );
multipart.addBodyPart ( messageBodyPart );
message.setContent ( multipart );
Transport.send ( message );
System.out.println ( "Message send successfully...." );
} catch ( Exception me ) {
me.printStackTrace ();
}
}
private void performTask () {
sendMail ( from, to, subject, messageBody, fileName );
}
public static void main ( String[] args ) {
new SendMailExample ().performTask ();
}
}
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
class SMTPAuthenticator extends Authenticator {
private static final String SMTP_AUTH_USER = "example@gmail.com";
private static final String SMTP_AUTH_PASSWORD = "somepassword";
public PasswordAuthentication getPasswordAuthentication () {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PASSWORD;
return new PasswordAuthentication( username, password );
}
}
You simply needed this mail.jar file.
To compile, simply write ( mail.jar
is present at C:\install\java\mail\mail.jar` location ):
javac -classpath .;C:\install\java\mail\mail.jar SendMailExample.java
To run, write:
java -classpath .;C:\install\java\mail\mail.jar SendMailExample
THis will do :-)
这篇关于Eclipse中的Java简单电子邮件程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!