可以的:
给一个我原来学的时候写的一个例子,也可能没考虑全,我是用weblogic当服务器的.你参考着用吧:
发邮件:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*; public class SimpleSendMessage { public static void main(String[] args) { String host = "smtp.163.com";
String to = "[email protected]";
String from = "[email protected]";
String subject = "mail test";
String messageText = "I am sending a message using JavaMail "; Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true"); //允许smtp校验 try {
Session session = Session.getInstance(props,new MailAuthenticator("name","pwd")); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
}
catch(MessagingException mex){
mex.printStackTrace();
System.out.println("can't send mail");
} } }
class MailAuthenticator extends Authenticator
{
private String UserName;
private String Password;
public MailAuthenticator(String UserName,String Password)
{
this.UserName = UserName;
this.Password = Password;
} public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.UserName,this.Password);
}
}接收邮件:
//邮件客户端,从邮件服务器中查看邮件import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;public class SimpleReceiveMessage
{
public static void main(String[] args) throws Exception
{
Properties props = new Properties();
String mailhost = "pop3.163.com"; props.put("mail.host", mailhost);
props.put("mail.store.protocol", "pop3"); Authenticator auth = new MailAuthenticator("xxxx", " xxxxx"); Session mail_session = Session.getDefaultInstance(props, null); //连接到服务器
Store pop_store = null;
try
{
pop_store = mail_session.getStore();
pop_store.connect("pop3.163.com", "xxxx", "xxxxx");
}
catch(MessagingException me)
{
System.err.println("连接服务器失败");
System.exit(1);
} //查看邮件
Folder inbox = pop_store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE); System.out.println("message:");
Message[] messages = inbox.getMessages(); for(int i = 0; i < messages.length; i++)
{
System.out.println(messages[i].getSubject());
}/*
System.out.println("folders:");
Folder[] children = inbox.list();
for(int i = 0; i < children.length; i++)
System.out.println(children[i].getName()); */
}
}//认证
class MailAuthenticator extends Authenticator
{
public MailAuthenticator(String UserName,String Password)
{
this.UserName = UserName;
this.Password = Password;
} public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.UserName,this.Password);
} private String UserName;
private String Password;
}
给一个我原来学的时候写的一个例子,也可能没考虑全,我是用weblogic当服务器的.你参考着用吧:
发邮件:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*; public class SimpleSendMessage { public static void main(String[] args) { String host = "smtp.163.com";
String to = "[email protected]";
String from = "[email protected]";
String subject = "mail test";
String messageText = "I am sending a message using JavaMail "; Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true"); //允许smtp校验 try {
Session session = Session.getInstance(props,new MailAuthenticator("name","pwd")); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
}
catch(MessagingException mex){
mex.printStackTrace();
System.out.println("can't send mail");
} } }
class MailAuthenticator extends Authenticator
{
private String UserName;
private String Password;
public MailAuthenticator(String UserName,String Password)
{
this.UserName = UserName;
this.Password = Password;
} public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.UserName,this.Password);
}
}接收邮件:
//邮件客户端,从邮件服务器中查看邮件import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;public class SimpleReceiveMessage
{
public static void main(String[] args) throws Exception
{
Properties props = new Properties();
String mailhost = "pop3.163.com"; props.put("mail.host", mailhost);
props.put("mail.store.protocol", "pop3"); Authenticator auth = new MailAuthenticator("xxxx", " xxxxx"); Session mail_session = Session.getDefaultInstance(props, null); //连接到服务器
Store pop_store = null;
try
{
pop_store = mail_session.getStore();
pop_store.connect("pop3.163.com", "xxxx", "xxxxx");
}
catch(MessagingException me)
{
System.err.println("连接服务器失败");
System.exit(1);
} //查看邮件
Folder inbox = pop_store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE); System.out.println("message:");
Message[] messages = inbox.getMessages(); for(int i = 0; i < messages.length; i++)
{
System.out.println(messages[i].getSubject());
}/*
System.out.println("folders:");
Folder[] children = inbox.list();
for(int i = 0; i < children.length; i++)
System.out.println(children[i].getName()); */
}
}//认证
class MailAuthenticator extends Authenticator
{
public MailAuthenticator(String UserName,String Password)
{
this.UserName = UserName;
this.Password = Password;
} public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.UserName,this.Password);
} private String UserName;
private String Password;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货