我正在尝试使用 JSP 发送邮件。我能够发送 html 文件,并且图像也显示在电子邮件中,但图像的对齐方式不正确。(即从理想位置重新定位的图像)。我正在尝试编辑css,但没有运气。
谢谢 。
package mail;
public class Emailer {
private static final String staticAbove = "<html><head>"
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">"
+ "<title>Insert title here</title>"
+ "</head><body>"
+ "<div class=\"background\" style=\"background-color: #d6d6d6; width: 510px; height: 980px; margin-left: 725px;\">"
+ "<font color=\"#868384\" size=\"2\" style=\"margin-left: 100px;\"><br>"
+ "If you are unable to see images <u><a href = \"emailer.html\">click here for HTML version</a></u>"
+ "</font>"
+ "<div class=\"foreground\" style=\"z-index: 5;\">"
+ "<div style=\"position: relative; left: 0; top: 0;\">"
+ + "</div></div>"
+ "<div class=\"whiteAboveGray\" style=\"background-color: white; margin-right: 15px; margin-left: 15px; margin-bottom: 15px; height: 650px; margin-top: -54px;\">"
+ "<div class=\"content\">"
+ "<h1> <font color=\"388293\" size=\"4\" face=\"TImes New Roman\" style=\"margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px;\">Dear Customer ,</font>"
+ "</h1>"
+ "<hr class=\"hr\" style=\"background-color: silver;\">"
+ "<font color=\"928888\">"
+ "<p align=\"justify\" style=\"margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px;\">"
+ "</p>"
+ "<p align=\"justify\" style=\"margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px;\">"
+ "<p align=\"justify\" style=\"margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px;\">"
+ "</font>";
;
private static final String dynamicContent = "<div class=\"dyanamicContent\">"
+
以上是html代码。有 3 个字符串。我在这里使用这些字符串..
package mail;
import java.util.List;
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;
import com.sun.mail.handlers.message_rfc822;
公共类 MailUtility {
private static final String STATUS_ARCHIVE = "archive";
private CoverLetter coverLetter;
List<MailPropertiesInfo> successfullPropertiesList;
private static final String EMAIL_ID = "";
private static final String PASSWORD = "";
public void sendSuccessMail(List<MailPropertiesInfo> successfullPropertiesList, String sendToEmailId, String propertyId)
{
Message message = initializeMailSetting(sendToEmailId);
coverLetter = new CoverLetter();
String messageForUSerHavingProperties = CoverLetter.getMessageforuserhavingproperties();
StringBuilder content = getContent(successfullPropertiesList);
try
{
message.setSubject("");
message.setContent(messageForUSerHavingProperties
+"<b><u>Your Property ID is: </b>"
+ propertyId+"</u>\n\n"+content,
"text/html");
Transport.send(message);
//out.println("<b>Done</b>");
}
catch(MessagingException me) {
me.printStackTrace();
}
}
private StringBuilder getContent(
List<MailPropertiesInfo> successfullPropertiesList) {
StringBuilder content = new StringBuilder();
int count = 0;
for(MailPropertiesInfo mailProp : successfullPropertiesList)
{
content.append(mailProp.toString());
content.append("\n\n");
count++;
if(count == 5)
{
break;
}
}
return content;
}
public void sendNoPropertiesMail(String sendToEmailId, String propertyId)
{
Message message = initializeMailSetting(sendToEmailId);
coverLetter = new CoverLetter();
String messageForUserNotHavingProperties = CoverLetter.getMessageforusernothavingproperties();
try
{
message.setSubject("Property Suggestion From DoMyDeal");
message.setContent(Emailer.getStaticabove()+Emailer.getDynamiccontent()+Emailer.getStaticbelow()
+"<b><u>Your Property ID is: </b>"
+propertyId,
"text/html");
Transport.send(message);
}
catch(MessagingException me) {
me.printStackTrace();
}
}
private Message initializeMailSetting(String sendToEmailId) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session123 = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(EMAIL_ID,PASSWORD);
}
});
Message message = null ;
try
{
message = new MimeMessage(session123);
message.setFrom(new InternetAddress("domydealportal@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(sendToEmailId));
}
catch (MessagingException e)
{
e.printStackTrace();
}
return message;
}
public CoverLetter getCoverLetter() {
return coverLetter;
}
public void setCoverLetter(CoverLetter coverLetter) {
this.coverLetter = coverLetter;
}
public List<MailPropertiesInfo> getSuccessfullPropertiesList() {
return successfullPropertiesList;
}
public void setSuccessfullPropertiesList(
List<MailPropertiesInfo> successfullPropertiesList) {
this.successfullPropertiesList = successfullPropertiesList;
}
}