我有以下用于发送邮件的代码,但是在我尝试的某些日子里它没有工作。我已经按照所有可能的描述来完成这项工作,但仍然没有办法。我使用javax邮件服务发送邮件,我也更新了所有依赖项,但日志将显示已调用邮件功能,但我不会收到任何邮件。
import com.dotdex.squattn.models.UserMail;
import com.dotdex.squattn.util.Utility;
import com.firebase.client.Firebase;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* An endpoint class we are exposing
*/
@Api(
name = "userMailApi",
version = "v1",
resource = "userMail",
namespace = @ApiNamespace(
ownerDomain = "squattn.dotdex.com",
ownerName = "squattn.dotdex.com",
packagePath = ""
)
)
public class UserMailEndpoint {
private static final Logger logger = Logger.getLogger(UserMailEndpoint.class.getName());
/**
* This inserts a new <code>UserMail</code> object.
*
* @param userMail The object to be added.
* @return The object to be added.
*/
@ApiMethod(name = "sendUserConfirmationMail")
public UserMail sendUserConfirmationMail(UserMail userMail) {
logger.info("Calling insertUserMail method");
if(sendMail(userMail))
{
userMail.setMailSent(true);
}
return userMail;
}
private boolean sendMail(UserMail userMail) {
//start sending the mail
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
String link = "<br><br><a href='" +Utility.APP_URL + "/verifyEmail?uid=" + userMail.getUserId() + "' style='color: #ffffff; text-decoration: none; margin: 0px; text-align: center; vertical-align: baseline; border: 4px solid #6fb3e0; padding: 4px 9px; font-size: 15px; line-height: 21px; background-color: #6fb3e0;'> Confirm </a>";
try {
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(Utility.TOP_HTML + link + Utility.BOTTOM_HTML, "text/html");
mp.addBodyPart(htmlPart);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("noreply@parkpal-1278.appspotmail.com","ParkPal App"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("jerryhanksokafor@gmail.com", userMail.getUserName()));
msg.setSubject("Verify Your Email.");
msg.setContent(mp);
Transport.send(msg);
new Firebase(Utility.APP_URL).child(Utility.USER_STRING).child(userMail.getUserId()).child("verifyMailSent").setValue(true);
return true;
} catch (AddressException e) {
// ...
e.printStackTrace();
return false;
} catch (MessagingException e) {
// ...
e.printStackTrace();
return false;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
}
}
}