嘿,我正在构建一个用户可以向某人发送电子邮件的应用程序。 用户在“编辑”字段中输入要向其发送电子邮件的人员的电子邮件ID,然后按下发送按钮,必须通过附件发送电子邮件。
我该怎么办???????
谷歌搜索后我真的很困惑。 谁能告诉我确切的方式另外,如果我的鳕鱼文件没有签名,我无法从模拟器发送电子邮件
提前致谢
答案 0 :(得分:2)
试试这个。
Address[] address = new Address[1];
try {
address[0] = new Address(email,name);
} catch (AddressException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] data = readFile();
Multipart multipart = new Multipart();
SupportedAttachmentPart attach = new SupportedAttachmentPart(multipart,
"application/x-example", "test.txt", data);
multipart.addBodyPart(attach);
Message msg = new Message();
// add the recipient list to the message
try {
msg.addRecipients(Message.RecipientType.TO, address);
// set a subject for the message
msg.setSubject("Mail from mobile");
msg.setContent(multipart);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Transport.send(msg);
} catch (MessagingException e) {
System.out.println(e.getMessage());
}
private static byte[] readFile() {
String fName ="file:///store/home/user/test.txt";
byte[] data = null;
FileConnection fconn = null;
DataInputStream is = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
is = fconn.openDataInputStream();
data = IOUtilities.streamToBytes(is);
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (null != is)
is.close();
if (null != fconn)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
return data;
}
答案 1 :(得分:0)
这是一个创建新电子邮件并在从我的项目BBSSH发送之前将其提交审核的工作示例。您不需要也可以删除的对话框/弹出窗口。在这个例子中,我们将位图作为参数,并将其转换为我们附加到电子邮件的PNG。将以类似方式附加不同的内容类型。
如果代码是无符号的,你应该可以从模拟器做任何事情;但是我认为电子邮件实际上不会被发送,因为模拟器本身没有连接到实际的邮件服务器。
/** * Sends feedback, optionally including the provided bitmap as an attachement. * * it is the caller's responsibility to ensure that this is invoked * in a properly synchronized manner. * * @param screenshot - if not null, this function prompts * the user to include the screenshot as an attachment. */ public static void sendFeedback(Bitmap screenshot) { ResourceBundle b = ResourceBundle.getBundle(BBSSHRResource.BUNDLE_ID, BBSSHRResource.BUNDLE_NAME); try { Multipart mp = new Multipart(); Message msg = new Message(); Address[] addresses = {new Address("recipient@example.com", "Recipient Name")}; if (screenshot == null || Dialog.ask(Dialog.D_YES_NO, b.getString(BBSSHRResource.MSG_FEEDBACK_INCLUDE_SCREENSHOT), Dialog.YES) == Dialog.NO) { } else { PNGEncodedImage img = PNGEncodedImage.encode(screenshot); SupportedAttachmentPart pt = new SupportedAttachmentPart(mp, img.getMIMEType(), "bbssh-screen.png", img.getData()); mp.addBodyPart(pt); msg.setContent(mp); } msg.addRecipients(RecipientType.TO, addresses); msg.setSubject("BBSSH Feedback"); Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(msg)); } catch (AddressException ex) { Logger.getLogger().log(10, "Unable to send feedback: " + ex.getMessage()); } catch (MessagingException ex) { Logger.getLogger().log(10, "Unable to send feedback: " + ex.getMessage()); } }
如果您想发送邮件而不是将其用于审核而不是Invoke.invokeApplication,则可以使用Transport.send(msg);