使用smtp协议来编写回复邮件和移动邮件(移动邮件:把收件箱的邮件移动到发件箱),转发邮件,求一例子,谢谢
解决方案 »
- SimpleDateFormat日期解析成1970年问题
- hibernate运行出现fail-safe cleanup (collections)信息是怎么一回事
- request问题
- spring mvc一个小例子,验证跳转后的错误!
- 关于XSLT的问题,急!在线等。如何在XSLT文件中设置一个变量,并且此变量在<xsl:for-each>的遍历过程中可以累加。
- 妈的 tomcat 5.0.28 不能正常运行!!!!!!!!!
- !!!!!!!!!JDBC2.0的问题,急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 高分求救!!!
- 有必要学J2EE吗?
- class expected什么意思?
- 【转】spring框架是否提高了可维护性?
- Java基础,求指点
package com.huy.SendMail;import java.util.*;import javax.mail.*;
import javax.mail.internet.*;import com.hyq.test.SimpleAuthenticator;
/**
* 回复邮件
* @author Administrator
* vvvv
*/
public class SendMail { private static String server = "smtp.126.com";// smtp主机名。
private static String from = "[email protected]";// 发送方邮件地址
private static String pw = "199217";// 发送方邮件密码。
private static String to = "[email protected]"; // 接收方邮件地址
private static final String PROTOCOL = "pop3";
static String mailContent = "你他妈是猪啊";// 邮件正文 private static final String HOST_NAME = "pop.126.com"; public static void send() throws Exception { try {
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", server);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pw);
props.put("mail.pop.host", HOST_NAME);
sendMailSession = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() // 邮件安全认证。
{
return new PasswordAuthentication(from, pw);
}
});
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(to));
String returnSub= returnSub(props,"565");
if(returnSub==null){
returnSub="hello";
}
newMessage.setSubject(returnSub);
newMessage.setSentDate(new Date());
mailContent += new Date().toLocaleString();
newMessage.setText(mailContent);
transport = sendMailSession.getTransport("smtp");
Transport.send(newMessage); } catch (MessagingException ex) { }
} public static void main(String[] args) throws Exception {
SendMail sml = new SendMail();
sml.send();
System.out.println("成功");
}
public static String returnSub(Properties props,String subject) throws Exception {
Session session = Session.getDefaultInstance(props,
new SimpleAuthenticator(from, pw));
Store store = session.getStore(PROTOCOL);
store.connect(HOST_NAME, from, pw);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = messages.length - 1; i >= 0; i--) {
System.out.println(messages[i].getSubject().toString());
if(messages[i].getSubject().toString().equals(subject)){
return "回复:"+messages[i].getSubject().toString();
}
}
return null;
}
}关于移动文件夹,希望大家多多支持啊,小弟还没有想出来啊