Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Xamarin.Forms应用程序中从Gmail发送电子邮件_C#_Xamarin.forms_Portable Class Library - Fatal编程技术网

C# 在Xamarin.Forms应用程序中从Gmail发送电子邮件

C# 在Xamarin.Forms应用程序中从Gmail发送电子邮件,c#,xamarin.forms,portable-class-library,C#,Xamarin.forms,Portable Class Library,我正在尝试使用Gmail从Xamarin表单应用程序发送电子邮件 我创建了一个只有1个方法的接口:sendmail() 然后,在Droid项目中,我添加了一个实现该接口的类。使用Dependency属性并在主项目中获得该方法的实现,除以下错误外,一切正常: Could not resolve host 'smtp.gmail.com' 这是该方法的实际实现: string subject = "subject here "; string body= "body here ";

我正在尝试使用Gmail从Xamarin表单应用程序发送电子邮件

我创建了一个只有1个方法的接口:sendmail()

然后,在Droid项目中,我添加了一个实现该接口的类。使用Dependency属性并在主项目中获得该方法的实现,除以下错误外,一切正常:

Could not resolve host 'smtp.gmail.com'
这是该方法的实际实现:

    string subject = "subject here ";
    string body= "body here ";
    try
    {
        var mail = new MailMessage();
        var smtpServer = new SmtpClient("smtp.gmail.com", 587);
        mail.From = new MailAddress("myEmailAddress@gmail.com");
        mail.To.Add("anotherAddress@yahoo.com");
        mail.Subject = subject;
        mail.Body = body;
        smtpServer.Credentials = new NetworkCredential("username", "pass");
        smtpServer.UseDefaultCredentials = false;   
        smtpServer.EnableSsl = true;
        smtpServer.Send(mail);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex);

    }
在四处搜索时,除了实际的smtp地址之外,我找不到任何关于它的详细信息


此外,我还使用了来自谷歌的不太安全的应用程序程序,没有收到凭据错误。我假设它可以连接到帐户。您好,我使用下面的代码实现了这一点,我还使用依赖项服务将一个文件附加到电子邮件中。我使用以下方法:

安卓:

public static string ICSPath
{
    get
    {
        var path = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, StaticData.CalendarFolderName);
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        return Path.Combine(path, StaticData.CalendarFileName);
    }
}

public async Task<bool> ShareCalendarEvent(List<ISegment> segmentList)
{
    Intent choserIntent = new Intent(Intent.ActionSend);

    //Create the calendar file to attach to the email
    var str = await GlobalMethods.CreateCalendarStringFile(segmentList);

    if (File.Exists(ICSPath))
    {
        File.Delete(ICSPath);
    }

    File.WriteAllText(ICSPath, str);

    Java.IO.File filelocation = new Java.IO.File(ICSPath);
    var path = Android.Net.Uri.FromFile(filelocation);

    // set the type to 'email'
    choserIntent.SetType("vnd.android.cursor.dir/email");
    //String to[] = { "asd@gmail.com" };
    //emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
    // the attachment
    choserIntent.PutExtra(Intent.ExtraStream, path);
    // the mail subject
    choserIntent.PutExtra(Intent.ExtraSubject, "Calendar event");
    Forms.Context.StartActivity(Intent.CreateChooser(choserIntent, "Send Email"));

    return true;

}
公共静态字符串路径
{
收到
{
var path=path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath,StaticData.CalendarFolderName);
如果(!Directory.Exists(path))
CreateDirectory(路径);
返回Path.Combine(路径,StaticData.CalendarFileName);
}
}
公共异步任务ShareCalendarEvent(列表分段列表)
{
Intent choserIntent=新的Intent(Intent.ActionSend);
//创建要附加到电子邮件的日历文件
var str=await GlobalMethods.CreateCalendarStringFile(segmentList);
if(File.Exists(ICSPath))
{
文件删除(ICSPath);
}
writealText(ICSPath,str);
Java.IO.File filelocation=新的Java.IO.File(ICSPath);
var path=Android.Net.Uri.FromFile(filelocation);
//将类型设置为“电子邮件”
choserIntent.SetType(“vnd.android.cursor.dir/email”);
//字符串到[]={”asd@gmail.com" };
//emailIntent.putExtra(Intent.EXTRA_电子邮件,收件人);
//附件
choserint.PutExtra(Intent.ExtraStream,path);
//邮件主题
choserIntent.PutExtra(Intent.ExtraSubject,“日历事件”);
Forms.Context.StartActivity(Intent.CreateChooser(choserContent,“发送电子邮件”));
返回true;
}
iOS:

公共静态字符串路径
{
收到
{
var path=path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal)、StaticData.CalendarFolderName);
如果(!Directory.Exists(path))
CreateDirectory(路径);
返回Path.Combine(路径,StaticData.CalendarFileName);
}
}
公共异步任务ShareCalendarEvent(列表分段列表)
{
//创建要附加到电子邮件的日历文件
var str=await GlobalMethods.CreateCalendarStringFile(segmentList);
if(File.Exists(ICSPath))
{
文件删除(ICSPath);
}
writealText(ICSPath,str);
MFMailComposeViewController邮件;
if(MFMailComposeViewController.CanSendMail)
{
mail=新的MFMailComposeViewController();
mail.SetSubject(“日历事件”);
//SetMessageBody(“这是一个测试”,false);
NSData t_dat=NSData.FromFile(ICSPath);
字符串t_fname=Path.GetFileName(ICSPath);
mail.AddAttachmentData(t_dat,@“text/v-calendar”,t_fname);
mail.Finished+=(对象s,mfcomposeSultEventArgs args)=>
{
//发送电子邮件后处理操作。
args.Controller.DismissViewController(true,null);
};
Device.beginInvokeMainThread(()=>
{
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(邮件,true,null);
});
}
其他的
{
//处理无法发送电子邮件的问题
等待App.BasePageReference.DisplayAlert(“不支持邮件”,
StaticData.ServiceUnavailable,StaticData.OK);
}
返回true;
}

我希望这能有所帮助。

您好,我已经使用下面的代码实现了这一点,我还使用依赖项服务将一个文件附加到电子邮件中,我使用以下方法:

安卓:

public static string ICSPath
{
    get
    {
        var path = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, StaticData.CalendarFolderName);
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        return Path.Combine(path, StaticData.CalendarFileName);
    }
}

public async Task<bool> ShareCalendarEvent(List<ISegment> segmentList)
{
    Intent choserIntent = new Intent(Intent.ActionSend);

    //Create the calendar file to attach to the email
    var str = await GlobalMethods.CreateCalendarStringFile(segmentList);

    if (File.Exists(ICSPath))
    {
        File.Delete(ICSPath);
    }

    File.WriteAllText(ICSPath, str);

    Java.IO.File filelocation = new Java.IO.File(ICSPath);
    var path = Android.Net.Uri.FromFile(filelocation);

    // set the type to 'email'
    choserIntent.SetType("vnd.android.cursor.dir/email");
    //String to[] = { "asd@gmail.com" };
    //emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
    // the attachment
    choserIntent.PutExtra(Intent.ExtraStream, path);
    // the mail subject
    choserIntent.PutExtra(Intent.ExtraSubject, "Calendar event");
    Forms.Context.StartActivity(Intent.CreateChooser(choserIntent, "Send Email"));

    return true;

}
公共静态字符串路径
{
收到
{
var path=path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath,StaticData.CalendarFolderName);
如果(!Directory.Exists(path))
CreateDirectory(路径);
返回Path.Combine(路径,StaticData.CalendarFileName);
}
}
公共异步任务ShareCalendarEvent(列表分段列表)
{
Intent choserIntent=新的Intent(Intent.ActionSend);
//创建要附加到电子邮件的日历文件
var str=await GlobalMethods.CreateCalendarStringFile(segmentList);
if(File.Exists(ICSPath))
{
文件删除(ICSPath);
}
writealText(ICSPath,str);
Java.IO.File filelocation=新的Java.IO.File(ICSPath);
var path=Android.Net.Uri.FromFile(filelocation);
//将类型设置为“电子邮件”
choserIntent.SetType(“vnd.android.cursor.dir/email”);
//字符串到[]={”asd@gmail.com" };
//emailIntent.putExtra(Intent.EXTRA_电子邮件,收件人);
//附件
choserint.PutExtra(Intent.ExtraStream,path);
//邮件主题
choserIntent.PutExtra(Intent.ExtraSubject,“日历事件”);
Forms.Context.StartActivity(Intent.CreateChooser(choserContent,“发送电子邮件”));
返回true;
}
iOS:

公共静态字符串路径
{
收到
{
var path=path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal)、StaticData.CalendarFolderName);
如果(!Directory.Exists(path))
CreateDirectory(路径);
返回Path.Combine(路径,StaticData.CalendarFileName);
}
}
公共异步任务ShareCalendarEvent(列表分段列表)
{
//创建要附加到电子邮件的日历文件
var str=await GlobalMethods.CreateCalendarStringFile(segmentList);
if(File.Exists(ICSPath))
{
文件删除(ICSPath);
}
writealText(ICSPath,str);
MFMailComposeViewController邮件;
如果(MFMailComposeViewControl