这是我在该框中提到的代码。所以请检查此代码并给我正确的答案。这段代码完全在我的系统中运行并成功运行并显示构建成功但邮件不发送邮件ids.so请帮助我
提前致谢
这是我的代码。
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Main1
{
String d_email = "xyz@ibm.com",
d_password = "*******",
d_host = "blr-outlook.ibm.com",
d_port = "25",
m_to = "xyz@ibm.com",
m_subject = "Testing",
m_text = "Hey, this is the testing email using blr-outlook.ibm.com";
public static void main(String[] args)
{
String[] to={"xyz@ibm.com"};
String[] cc={"xyz@ibm.com"};
String[] bcc={"xyz@ibm.com"};
//This is for google
Main1.sendMail("xyz@ibm.com", "password", "blr-outlook.ibm.com",
"25", "true", "true",
true, "javax.net.ssl.SSLSocketFactory", "false",
to, cc, bcc,
"hi baba don't send virus mails..",
"This is my style...of reply..If u send virus mails..");
}
public synchronized static boolean sendMail(
String userName, String passWord, String host,
String port, String starttls, String auth,
boolean debug, String socketFactoryClass, String fallback,
String[] to, String[] cc, String[] bcc,
String subject, String text)
{
Properties props = new Properties();
//Properties props=System.getProperties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
if(!"".equals(port)) {
props.put("mail.smtp.port", port);
}
if(!"".equals(starttls)) {
props.put("mail.smtp.starttls.enable",starttls);
}
props.put("mail.smtp.auth", auth);
if(debug) {
props.put("mail.smtp.debug", "true");
} else {
props.put("mail.smtp.debug", "false");
}
if(!"".equals(port)) {
props.put("mail.smtp.socketFactory.port", port);
}
if(!"".equals(socketFactoryClass)) {
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
}
if(!"".equals(fallback)) {
props.put("mail.smtp.socketFactory.fallback", fallback);
}
try
{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("blr-outlook.ibm.com"));
for(int i=0;i<to.length;i++) {
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(to[i]));
}
for(int i=0;i<cc.length;i++) {
msg.addRecipient(Message.RecipientType.CC,
new InternetAddress(cc[i]));
}
for(int i=0;i<bcc.length;i++) {
msg.addRecipient(Message.RecipientType.BCC,
new InternetAddress(bcc[i]));
}
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
}
catch (Exception mex)
{
return false;
}
}
}
答案 0 :(得分:0)
网上有很多例子......甚至教程点都有很好的解释例子....在这里查看.....
http://www.tutorialspoint.com/javamail_api/javamail_api_overview.htm
我已经使用java mail api进行邮寄,而且工作得很好.....我离家时所以我现在无法与你分享代码......
编辑:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "username@gmail.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
尝试这一点....身份验证不是强制性的....如果您使用任何有点应用程序,您需要更改您的Gmail帐户设置,以允许访问不太安全的应用程序....