use of i2p.susi.webmail.smtp.SMTPClient in project i2p.i2p by i2p.
the class WebMail method sendMail.
/**
* @param sessionObject
* @param request
* @return success
*/
private static boolean sendMail(SessionObject sessionObject, RequestWrapper request) {
boolean ok = true;
String from = request.getParameter(NEW_FROM);
String to = request.getParameter(NEW_TO);
String cc = request.getParameter(NEW_CC);
String bcc = request.getParameter(NEW_BCC);
String subject = request.getParameter(NEW_SUBJECT, _t("no subject"));
String text = request.getParameter(NEW_TEXT, "");
boolean fixed = Boolean.parseBoolean(Config.getProperty(CONFIG_SENDER_FIXED, "true"));
if (fixed) {
String domain = Config.getProperty(CONFIG_SENDER_DOMAIN, "mail.i2p");
from = "<" + sessionObject.user + "@" + domain + ">";
}
ArrayList<String> toList = new ArrayList<String>();
ArrayList<String> ccList = new ArrayList<String>();
ArrayList<String> bccList = new ArrayList<String>();
ArrayList<String> recipients = new ArrayList<String>();
String sender = null;
if (from == null || !Mail.validateAddress(from)) {
ok = false;
sessionObject.error += _t("Found no valid sender address.") + '\n';
} else {
sender = Mail.getAddress(from);
if (sender == null || sender.length() == 0) {
ok = false;
sessionObject.error += _t("Found no valid address in \\''{0}\\''.", quoteHTML(from)) + '\n';
}
}
ok = Mail.getRecipientsFromList(toList, to, ok);
ok = Mail.getRecipientsFromList(ccList, cc, ok);
ok = Mail.getRecipientsFromList(bccList, bcc, ok);
recipients.addAll(toList);
recipients.addAll(ccList);
recipients.addAll(bccList);
String bccToSelf = request.getParameter(NEW_BCC_TO_SELF);
boolean toSelf = "1".equals(bccToSelf);
// save preference in session
sessionObject.bccToSelf = toSelf;
if (toSelf)
recipients.add(sender);
if (toList.isEmpty()) {
ok = false;
sessionObject.error += _t("No recipients found.") + '\n';
}
Encoding qp = EncodingFactory.getEncoding("quoted-printable");
Encoding hl = EncodingFactory.getEncoding("HEADERLINE");
if (qp == null) {
ok = false;
// can't happen, don't translate
sessionObject.error += "Internal error: Quoted printable encoder not available.";
}
if (hl == null) {
ok = false;
// can't happen, don't translate
sessionObject.error += "Internal error: Header line encoder not available.";
}
long total = text.length();
boolean multipart = sessionObject.attachments != null && !sessionObject.attachments.isEmpty();
if (multipart) {
for (Attachment a : sessionObject.attachments) {
total += a.getSize();
}
}
if (total > SMTPClient.BINARY_MAX_SIZE) {
ok = false;
sessionObject.error += _t("Email is too large, max is {0}", DataHelper.formatSize2(SMTPClient.BINARY_MAX_SIZE, false) + 'B') + '\n';
}
if (ok) {
StringBuilder body = new StringBuilder(1024);
I2PAppContext ctx = I2PAppContext.getGlobalContext();
body.append("Date: " + RFC822Date.to822Date(ctx.clock().now()) + "\r\n");
// todo include real names, and headerline encode them
body.append("From: " + from + "\r\n");
Mail.appendRecipients(body, toList, "To: ");
Mail.appendRecipients(body, ccList, "Cc: ");
try {
body.append(hl.encode("Subject: " + subject.trim()));
} catch (EncodingException e) {
ok = false;
sessionObject.error += e.getMessage();
}
String boundary = "_=" + ctx.random().nextLong();
if (multipart) {
body.append("MIME-Version: 1.0\r\nContent-type: multipart/mixed; boundary=\"" + boundary + "\"\r\n\r\n");
} else {
body.append("MIME-Version: 1.0\r\nContent-type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n");
}
try {
// TODO pass the text separately to SMTP and let it pick the encoding
if (multipart)
body.append("--" + boundary + "\r\nContent-type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n");
body.append(qp.encode(text));
} catch (EncodingException e) {
ok = false;
sessionObject.error += e.getMessage();
}
// set to the StringBuilder so SMTP can replace() in place
sessionObject.sentMail = body;
if (ok) {
SMTPClient relay = new SMTPClient();
if (relay.sendMail(sessionObject.host, sessionObject.smtpPort, sessionObject.user, sessionObject.pass, sender, recipients.toArray(new String[recipients.size()]), sessionObject.sentMail, sessionObject.attachments, boundary)) {
sessionObject.info += _t("Mail sent.");
sessionObject.sentMail = null;
sessionObject.clearAttachments();
} else {
ok = false;
sessionObject.error += relay.error;
}
}
}
return ok;
}
Aggregations