如何通过sahi UI发送带附件的电子邮件

时间:2014-11-10 04:35:21

标签: sahi

我正在尝试通过SAHI UI向附件发送电子邮件。但我无法这样做。有人能帮我吗。我用来发送电子邮件的代码如下:

function sendEmailWithAttachment($emailUsername, $emailPassword, $toEmail, $emailSubject, $emailBody) 
{
  var $host = "smtp.gmail.com";
  var $port = 465;
  var $isSSL = true; // set to true if you use SSL
  var $mailer = new Packages.net.sf.sahi.ant.Mailer($host, $port, $emailUsername, $emailPassword, $isSSL);
  var $from = $emailUsername;
  var $to = $toEmail;
  $mailer.addAttachment("3_PO#POS-POS-008-003358 DELL.pdf", "../../../../../lib/VSAAutomation.Common/3_PO#POS-POS-008-003358 DELL.pdf");
  $mailer.send($from, $to, $emailSubject, $emailBody);
}

我以这种方式调用函数:

sendEmailWithAttachment($notifyEmailId, $notifyEmailPassword, $emailReaderId, "automateticket"+$randomNum, "automated ticket from Sahi");

也是为了让您知道我已将mail.jar,activation.jar和ant-sahi.jar文件放在正确的位置

1 个答案:

答案 0 :(得分:1)

请使用这些功能发送带附件的电子邮件。您可以在下面的代码中提供附件。

function sendEmailWithProps($emailSubject, $emailBody) {
  var $props = loadProperties(_resolvePath("../config/email.properties"), false);
  var $mailer = new Packages.net.sf.sahi.ant.Mailer($props);
  var $subjectPrefix = $props.getProperty("mail.subject.prefix");
  if($subjectPrefix !== null){
$emailSubject = $subjectPrefix + $emailSubject;
  }
$mailer.addBody($emailBody);
$mailer.addSubject($emailSubject);

//Provide attachment details here
$mailer.addAttachment("attached", "c:/abc.txt");
$mailer.send();
}

function loadProperties($path, $isXML) {
  var $props = new java.util.Properties();
  try {
    var $inStream = new java.io.FileInputStream($path);
    if ($isXML) {
      $props.loadFromXML($inStream);
    } else {
      $props.load($inStream);
    }
    $inStream.close();
  } catch (e) {
            _log(e);
  }
  return $props;
}
sendEmailWithProps("Mail from Sahi", "All izz well");

请注意,您需要在userdata / config文件夹中拥有完全配置的email.properties文件。这有点棘手,因为您需要使用属性文件来发送带附件的电子邮件。