|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
先谈谈我对java的一些认识。我选择java,是因为他语法简单,功能强大,从web,到桌面,到嵌入式,无所不能。但当我进一步了解了java后,感叹,java原来也有许多缺点。网上良多,不外一样平常都是没有smtp考证的,下边是一段示例代码:
不克不及间接运转的,不外,能够看看内里关于考证的部分。
//发送邮件函数
publicbooleansendMail(StringmailTo,StringmailSubject,StringmailBody){
//发送email
try{
//defaultaccountinformation
StringsmtpServer="smtp.smtpserver.com";
StringsmtpAuth="true";
StringsmtpUser="username";
StringsmtpPassword="password";
StringFrom="from@yourserver.com";
StringTo=mailTo;
StringSubject=mailSubject;
StringText=mailBody;
java.util.ResourceBundleresBundle;
resBundle=java.util.ResourceBundle.getBundle("mailinfo",
Locale.SIMPLIFIED_CHINESE);
if(resBundle!=null){
smtpServer=resBundle.getString("mail.smtp.host");
smtpAuth=resBundle.getString("mail.smtp.auth");
smtpUser=resBundle.getString("mail.smtp.user");
smtpPassword=resBundle.getString("mail.smtp.password");
From=resBundle.getString("mail.smtp.from");
}
Propertiesprops=newProperties();
SessionsendMailSession;
Transporttransport;
props.put("mail.smtp.host",smtpServer);
props.put("mail.smtp.auth",smtpAuth);
if("true".equals(smtpAuth)){
//smtp服务器必要考证,用MyAuthertiactor来创立mailsession
MyAuthenticatormyauth=newMyAuthenticator(smtpUser,smtpPassword);
sendMailSession=Session.getInstance(props,myauth);
}
else{
sendMailSession=Session.getInstance(props);
}
//Debug
sendMailSession.setDebug(true);
MessagenewMessage=newMimeMessage(sendMailSession);
newMessage.setFrom(newInternetAddress(From));
newMessage.setRecipient(Message.RecipientType.TO,
newInternetAddress(mailTo));
newMessage.setSubject(Subject);
newMessage.setSentDate(newDate());
newMessage.setText(Text);
newMessage.saveChanges();
transport=sendMailSession.getTransport("smtp");
transport.send(newMessage,newMessage.getAllRecipients());
transport.close();
}
catch(ExceptionmailEx){
System.err.println("SendMailError:"+mailEx.getMessage());
returnfalse;
}
returntrue;
}
//smtp必要考证时分的考证类
classMyAuthenticator
extendsjavax.mail.Authenticator{
privateStringstrUser;
privateStringstrPwd;
publicMyAuthenticator(Stringuser,Stringpassword){
this.strUser=user;
this.strPwd=password;
}
protectedPasswordAuthenticationgetPasswordAuthentication(){
returnnewPasswordAuthentication(strUser,strPwd);
}
}
手机用到的是用j2me所编出来的小程序。 |
|