如何在Qt和Qml中发邮件

原创帖,转发请注明出处:http://thierry-xing.iteye.com/blog/1407453

 

有两个方法可以在Qt中发邮件:
1) 使用QtMobility API

 

#include <QMessage>
#include <QMessageService>

QMessageService* msgAction = new QMessageService(this);
QMessage msg;
msg.setType(QMessage::Email);
msg.setTo(QMessageAddress(QMessageAddress::Email,"[email protected]"));
msg.setSubject("邮件标题");
msg.setBody("邮件内容");
QStringList attachments;
attachments.append(":/res/icon.png");
msg.appendAttachments(attachments);
//Send email message
qDebug()<<"Sending Status = "<<msgAction->send(msg);

 

2) 使用默认邮件客户端

QDesktopServices:openUrl(QUrl(tr("mailto:[email protected]?subject=邮件标题&body=邮件内容")));

 

qml中这样使用:

Qt.openUrlExternally("mailto:[email protected]?subject=邮件标题&body=邮件内容)

你可能感兴趣的:(qt)