Flutter-使用Google API发送电子邮件 [英] Flutter - Send email using Google API
问题描述
经过大量研究和修复问题后,我到达了 github 中的以下位置。但是我不知道我是否正确安装了json。对于出现以下错误:
After much study and repairing the problems, I arrived at the following location that is in my github. But I do not know if I'm mounting json properly. For the following error is appearing:
{
error:
{
errors: [
{
domain: global,
reason: parseError,
message: This API does not support parsing form-encoded input.
}
],
code: 400,
message: This API does not support parsing form-encoded input.
}
}
我按照以下步骤设置帖子更多详细信息,该项目位于我的 github
I'm setting up the post as follows, for more details the project is in my github
// scope for send email
GoogleSignIn googleSignIn = new GoogleSignIn(
scopes: <String>[
'https://www.googleapis.com/auth/gmail.send'
],
);
await googleSignIn.signIn().then((data) {
testingEmail(data.email, data.authHeaders);
});
// userId is the email
Future<Null> testingEmail(userId, header) async {
String url = 'https://www.googleapis.com/gmail/v1/users/' + userId + '/messages/send';
final http.Response response = await http.post(
url,
headers: await header,
body: {
'from': userId,
'to': userId,
'subject': 'testing send email',
'text': 'worked!!!'
}
);
}
我在做什么错了,无法通过电子邮件发送电子邮件Google API?您能帮我解决这个问题吗?
What am I doing wrong, to not be able to send an email through the Google API? Could you help me with this problem?
推荐答案
进行了一些更改,主要的更改是http帖子主体需要用 raw
键将其作为json,并将其内容包含在 base64
中,并且已转换为base64的文本必须为MIMEText,即特定格式,如下所示。
A few changes were made, the main one being that the http post body needed to be a json with the raw
key and its contents in base64
And this text that has been converted to base64 must be a MIMEText, so the specific format, as below.
要将html更改为文本,只需更改 Content-Type:text / html
从字符串到 Content-Type:text / plain
To change the html to text simply change the Content-Type: text/html
from the string toContent-Type: text/plain
以下是码。完整的代码在 github
The following is a clipping of the code. The complete code is in github
await googleSignIn.signIn().then((data) {
data.authHeaders.then((result) {
var header = {'Authorization': result['Authorization'], 'X-Goog-AuthUser': result['X-Goog-AuthUser']};
testingEmail(data.email, header);
});
});
Future<Null> testingEmail(String userId, Map header) async {
header['Accept'] = 'application/json';
header['Content-type'] = 'application/json';
var from = userId;
var to = userId;
var subject = 'test send email';
//var message = 'worked!!!';
var message = "Hi<br/>Html Email";
var content = '''
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
to: ${to}
from: ${from}
subject: ${subject}
${message}''';
var bytes = utf8.encode(content);
var base64 = base64Encode(bytes);
var body = json.encode({'raw': base64});
String url = 'https://www.googleapis.com/gmail/v1/users/' + userId + '/messages/send';
final http.Response response = await http.post(
url,
headers: header,
body: body
);
if (response.statusCode != 200) {
setState(() {
print('error: ' + response.statusCode.toString());
});
return;
}
final Map<String, dynamic> data = json.decode(response.body);
print('ok: ' + response.statusCode.toString());
}
这篇关于Flutter-使用Google API发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!