import javautilProperties;
import javautilDate;
import javaxmailSession;
import javaxmailMessage;
import javaxmailTransport;
import javaxmailinternetMimeMessage;
import javaxmailinternetInternetAddress;
public class HelloWorldJavaMail {
public static void main(String[] args) {
try {
Properties props = SystemgetProperties();
propsput("mailsmtphost","xxxxxxxxxxxx");
Session session=SessiongetDefaultInstance(props,null);
MimeMessage mimeMessage=new MimeMessage(session);
mimeMessagesetFrom(new InternetAddress("xx@xxxx","姓名","gb2312"));
mimeMessagesetRecipients(MessageRecipientTypeTO,"xx@xxxx");
mimeMessagesetSubject("Hello World JavaMail","gb2312");
mimeMessagesetText("<h1>Hello World JavaMail</h1>","gb2312");
mimeMessagesetHeader("Content-Type","text/html");
mimeMessagesetSentDate(new Date());
Transportsend(mimeMessage);
} catch (Exception e) {
eprintStackTrace();
}
}
}
2。收信
import javaxmail;
import javaxmailinternet;
import javautil;
public class HelloWorldPOP {
public static void main(String args[]) {
try {
String host="xxxxxxxxxxxx";
String user="xxxxxx";
String password="xxxxxx";
Session session = SessiongetDefaultInstance(SystemgetProperties(), null);
Store store = sessiongetStore("pop3");
storeconnect(host, -1, user, password);
Folder folder = storegetFolder("INBOX");
folderopen(FolderREAD_ONLY);
int totalMessages = foldergetMessageCount();
if (totalMessages == 0) {
Systemoutprintln("没有邮件");
folderclose(false);
storeclose();
return;
}
Message[] messages = foldergetMessages();
for (int i = 0; i < messageslength; i++) {
showMessage(messages[i]);
}
folderclose(false);
storeclose();
} catch (Exception e) {
eprintStackTrace();
}
}
public static void showMessage(Message m) throws Exception {
Address[] a;
if ((a = mgetFrom())!=null) {
for (int j = 0; j < alength; j++) {
Systemoutprintln("送信人: " + MimeUtilitydecodeText(a[j]toString()));
}
}
if ((a = mgetRecipients(MessageRecipientTypeTO))!=null) {
for (int j = 0; j < alength; j++) {
Systemoutprintln("收信人: " + MimeUtilitydecodeText(a[j]toString()));
}
}
Systemoutprintln("主体: " + mgetSubject());
Date d = mgetSentDate();
Systemoutprintln("日期: " + (d!= null dtoString() : "不明"));
Systemoutprintln("大小: " + mgetSize());
Systemoutprintln("内容:\n" + mgetContent());
}
}
用JAVAMAIL很容易实现的。
参照:
>
public boolean sendout()
{
try
{
mimeMessagesetContent(mp);
mimeMessagesaveChanges();
Session mailSession = SessiongetInstance(props, null);
Transport transport = mailSessiongetTransport("smtp");
transportconnect(hostname, username, password);
transportsendMessage(mimeMessage, mimeMessagegetRecipients(javaxmailMessageRecipientTypeTO));
transportclose();
}
catch(Exception e)
{
eprintStackTrace();
return false;
}
return true;
}
以上就是关于用java写收发邮件的程序,求助,在线全部的内容,包括:用java写收发邮件的程序,求助,在线、java 发送邮件、java程序发送邮件报错等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)