Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. Could not resolve host 'smtp.gmail.com'
  2.  
  3. string subject = "subject here ";
  4. string body= "body here ";
  5. try
  6. {
  7. var mail = new MailMessage();
  8. var smtpServer = new SmtpClient("smtp.gmail.com", 587);
  9. mail.From = new MailAddress("myEmailAddress@gmail.com");
  10. mail.To.Add("anotherAddress@yahoo.com");
  11. mail.Subject = subject;
  12. mail.Body = body;
  13. smtpServer.Credentials = new NetworkCredential("username", "pass");
  14. smtpServer.UseDefaultCredentials = false;
  15. smtpServer.EnableSsl = true;
  16. smtpServer.Send(mail);
  17. }
  18. catch (Exception ex)
  19. {
  20. System.Diagnostics.Debug.WriteLine(ex);
  21.  
  22. }
  23.  
  24. public static string ICSPath
  25. {
  26. get
  27. {
  28. var path = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, StaticData.CalendarFolderName);
  29. if (!Directory.Exists(path))
  30. Directory.CreateDirectory(path);
  31. return Path.Combine(path, StaticData.CalendarFileName);
  32. }
  33. }
  34.  
  35. public async Task<bool> ShareCalendarEvent(List<ISegment> segmentList)
  36. {
  37. Intent choserIntent = new Intent(Intent.ActionSend);
  38.  
  39. //Create the calendar file to attach to the email
  40. var str = await GlobalMethods.CreateCalendarStringFile(segmentList);
  41.  
  42. if (File.Exists(ICSPath))
  43. {
  44. File.Delete(ICSPath);
  45. }
  46.  
  47. File.WriteAllText(ICSPath, str);
  48.  
  49. Java.IO.File filelocation = new Java.IO.File(ICSPath);
  50. var path = Android.Net.Uri.FromFile(filelocation);
  51.  
  52. // set the type to 'email'
  53. choserIntent.SetType("vnd.android.cursor.dir/email");
  54. //String to[] = { "asd@gmail.com" };
  55. //emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
  56. // the attachment
  57. choserIntent.PutExtra(Intent.ExtraStream, path);
  58. // the mail subject
  59. choserIntent.PutExtra(Intent.ExtraSubject, "Calendar event");
  60. Forms.Context.StartActivity(Intent.CreateChooser(choserIntent, "Send Email"));
  61.  
  62. return true;
  63.  
  64. }
  65.  
  66. public static string ICSPath
  67. {
  68. get
  69. {
  70. var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), StaticData.CalendarFolderName);
  71. if (!Directory.Exists(path))
  72. Directory.CreateDirectory(path);
  73. return Path.Combine(path, StaticData.CalendarFileName);
  74. }
  75. }
  76.  
  77.  
  78. public async Task<bool> ShareCalendarEvent(List<ISegment> segmentList)
  79. {
  80. //Create the calendar file to attach to the email
  81. var str = await GlobalMethods.CreateCalendarStringFile(segmentList);
  82.  
  83. if (File.Exists(ICSPath))
  84. {
  85. File.Delete(ICSPath);
  86. }
  87.  
  88. File.WriteAllText(ICSPath, str);
  89.  
  90. MFMailComposeViewController mail;
  91. if (MFMailComposeViewController.CanSendMail)
  92. {
  93. mail = new MFMailComposeViewController();
  94. mail.SetSubject("Calendar Event");
  95. //mail.SetMessageBody("this is a test", false);
  96. NSData t_dat = NSData.FromFile(ICSPath);
  97. string t_fname = Path.GetFileName(ICSPath);
  98. mail.AddAttachmentData(t_dat, @"text/v-calendar", t_fname);
  99.  
  100. mail.Finished += (object s, MFComposeResultEventArgs args) =>
  101. {
  102. //Handle action once the email has been sent.
  103. args.Controller.DismissViewController(true, null);
  104. };
  105.  
  106. Device.BeginInvokeOnMainThread(() =>
  107. {
  108. UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(mail, true, null);
  109. });
  110.  
  111. }
  112. else
  113. {
  114. //Handle not being able to send email
  115. await App.BasePageReference.DisplayAlert("Mail not supported",
  116. StaticData.ServiceUnavailble, StaticData.OK);
  117. }
  118.  
  119. return true;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement