这是可能的,但您需要在DNS中查找自己的MX记录.您可以使用
dnsjava-2.16.jar库.添加dnsjava和javax.mail库以构建编译以下代码示例的路径.尝试以这种方式传递邮件后检查垃圾邮件文件夹:因为没有DKIM签名,并且用于发送电子邮件的IP地址不属于此主机允许的地址(“yourhost.org”),接收邮件服务器可能将电子邮件放入垃圾邮件文件夹.
import com.sun.mail.smtp.SMTPSendFailedException;
import com.sun.mail.smtp.SMTPSenderFailedException;
import com.sun.mail.util.MailConnectException;
import org.xbill.DNS.*;
import javax.mail.Message;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SendEmail {
String Subject = "Email subject!";
Template template;
Properties properties;
String hostname = "yourhost.org"; // the host to send email from
String to;
String fromEmal;
String fromName = "Your name";
SendEmail(String from,String to){
this.to = to;
this.fromEmal = from;
this.properties = new Properties();
this.properties.put("mail.transport.protocol", "smtp");
this.properties.put("mail.smtp.host", getMXRecordsForEmailAddress(to)); // SMTP Server
this.properties.put("mail.smtp.port","25");
this.properties.put("mail.smtp.localhost", hostname); // HELO host
this.properties.put("mail.smtp.from",from);// SMTP MAIL FROM
this.properties.put("mail.smtp.allow8bitmime","true");
// this.properties.put("mail.smtp.localaddress","192.168.1.44"); // Connect from this IP
sendMessage();
}
public void sendMessage() {
try{
Session session = Session.getInstance(this.properties);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("\""+this.fromName+"\""+""));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));
msg.setSubject(this.Subject);
msg.setContent("Email body with HTML","text/html; charset=utf-8");
Transport.send(msg);
System.out.println("Message sent OK");
}catch (AddressException e){
System.out.println("Bad address format: "+e.getRef());
}catch (SMTPSenderFailedException e){
System.out.println(e.getReturnCode() + ": We can't send emails from this address: " + e.getAddress());
}catch (NoSuchProviderException e){
System.out.println(e.getMessage()+": No such provider");
}catch (SMTPSendFailedException e){
System.out.print(e.getReturnCode() + ": " + e.getMessage());
}catch (MailConnectException e){
System.out.println("Can't connect to "+ e.getHost()+":"+e.getPort());
}
catch(MessagingException e){
System.out.println("Unknown exception"+e);
}catch (Exception e){
System.out.println(e);
}
}
public String getMXRecordsForEmailAddress(String eMailAddress) {
String returnValue = new String();
try {
String parts[] = eMailAddress.split("@");
String hostName = parts[1];
Record[] records = new Lookup(hostName, Type.MX).run();
if (records == null) { throw new RuntimeException("No MX records found for domain " + hostName + "."); }
if (records.length > 0){
MXRecord mxr = (MXRecord) records[0];
for (int i = 0; i< records.length; i++){
MXRecord tocompare = (MXRecord)records[i];
if (mxr.getPriority() > tocompare.getPriority())
mxr = tocompare;
}
returnValue = mxr.getTarget().toString();
}
} catch (TextParseException e) {
return new String("NULL");
}
return returnValue;
}
}